用PANDA打开CSV文件,该文件在另一个路径中的另一个主文件中带有相对路径

发布于 2025-01-25 16:04:47 字数 1379 浏览 1 评论 0原文

我的问题类似于链接但是有一个微小的区别,

结构是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2025-02-01 16:04:47
 handle = open(
FileNotFoundError: [Errno 2] No such file or directory: '../data_folder/data.csv'

这表明它应该在scripts_folder中工作。
../ data_folder表示相对于当前的目录获取目录,并在此目录中找到data_folder

您可以使用csv-file使用os.path.abspathos.path.join os.getCWD()

 handle = open(
FileNotFoundError: [Errno 2] No such file or directory: '../data_folder/data.csv'

This indicates that it supposed to work inside scripts_folder.
../data_folder means get the directory up relative to the current and find data_folder in this directory.

You could construct absolute file path for csv-file using os.path.abspath and os.path.join and give it to script reading data.
To debug current working directory for script, use os.getcwd()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文