File location macro for python (Jupyter notebook)

huangapple go评论64阅读模式
英文:

File location macro for python (Jupyter notebook)

问题

我是Python/Jupyter笔记本新手。有没有办法将文件位置保存为宏,以便我在每次打开来自同一文件夹的数据集时不必复制和粘贴相同的位置?

dat = pd.read_csv("C:/Users/Box Sync/real_world_validation/Data/mydata.csv")

我尝试了以下代码,但没有成功:

data = "C:/Users/Box Sync/real_world_validation/Data"
dat = pd.read_csv("data/mydata.csv")
英文:

I am new to python/Jupyter notebook. Is there a way I can save the file location as a macro so that I don't need to copy and paste the same location each time I open a dataset from the same folder?

dat = pd.read_csv("C:/Users/Box Sync/real_world_validation/Data/mydata.csv")

I tried the following code, but did not work:

data = "C:/Users/Box Sync/real_world_validation/Data"
dat = pd.read_csv("data/mydata.csv")

答案1

得分: 0

import os
os.chdir(r"C:/Users/Box Sync/real_world_validation/Data")
dat = pd.read_csv("mydata.csv")

英文:

Or if your notebook will always be working in 1 directory you could change the current working directory altogether

import os
os.chdir(r"C:/Users/Box Sync/real_world_validation/Data")
dat = pd.read_csv("mydata.csv")

答案2

得分: 0

Sure, here's the translation of the code part:

要访问位于相同文件夹中的数据我在这里发布 @abhilb 的答案

path = "C:/Users/Box Sync/real_world_validation/Data"
data = pd.read_csv(path + "/mydata.csv")
英文:

To get access to the data in the same folder, I post the answer by @abhilb here:

path = "C:/Users/Box Sync/real_world_validation/Data"
data = pd.read_csv(path + "/mydata.csv")

huangapple
  • 本文由 发表于 2020年1月3日 23:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定