用PANDA打开CSV文件,该文件在另一个路径中的另一个主文件中带有相对路径
我的问题类似于链接但是有一个微小的区别,
结构是
project_folder
--> main.py
--> scripts_folder
--> script.py
--> data_folder
--> data.csv
main.py看起来像这样
from scripts_folder.script import a
a()
。
import pandas as pd
df = pd.read_csv('../data_folder/data.csv')
# do some things with df
def a():
print('something')
项目 .py ,必须从data.csv
导入数据。 我尝试了上述问题中给出的方法。即,我尝试在scripty.py
pd.ReAC_CSV('../ data_folder/data.csv')
中尝试使用此代码行导入。
它给了我错误
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: '../data_folder/data.csv'
[编辑]
我尝试将data.csv
移至scripts_folder
in in in in in in on in scripts_folder
project_folder
--> main.py
--> scripts_folder
--> script.py
--> data.csv
--> data_folder (empty)
,然后将其更改为df = pd.pd.read_csv('数据。 CSV')
,但我仍然遇到同样的错误,
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'
有人可以帮助吗?谢谢
My question is similar to the one in link but with a slight difference
the project structure is
project_folder
--> main.py
--> scripts_folder
--> script.py
--> data_folder
--> data.csv
The main.py looks like this
from scripts_folder.script import a
a()
The script.py looks like this
import pandas as pd
df = pd.read_csv('../data_folder/data.csv')
# do some things with df
def a():
print('something')
I am running main.py
, which calls script.py
, which has to import data from data.csv
.
I tried the method given in the question above. ie I tried importing using this line of code in scripty.py
pd.reac_csv('../data_folder/data.csv')
but it is not working.
It is giving me error
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: '../data_folder/data.csv'
[EDIT]
I tried moving the data.csv
into scripts_folder
as in
project_folder
--> main.py
--> scripts_folder
--> script.py
--> data.csv
--> data_folder (empty)
and I changed it to df = pd.read_csv('data.csv')
and still I am getting the same error
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'
Can someone help? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这表明它应该在
scripts_folder
中工作。../ data_folder
表示相对于当前的目录获取目录,并在此目录中找到data_folder
。您可以使用
csv-file
使用os.path.abspath
和os.path.join
os.getCWD()This indicates that it supposed to work inside
scripts_folder
.../data_folder
means get the directory up relative to the current and finddata_folder
in this directory.You could construct absolute file path for
csv-file
usingos.path.abspath
andos.path.join
and give it to script reading data.To debug current working directory for script, use
os.getcwd()