Atom中打开的文件有问题,而代码只能在Python中工作

2024-06-28 11:28:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是: 我想处理一个名为最佳与.csv. 所以我写了一些代码来打开这个文件:

bestand = open("bestand.csv")

当我想在Atom中运行代码时,我得到以下消息:

 Traceback (most recent call last):
 File "A:\Drive\Fahrzeugverwaltung\Fahrzeugverwaltung.py", line 1, in 
 <module>
 bestand = open("bestand.csv")
 FileNotFoundError: [Errno 2] No such file or directory: 'bestand.csv'
 [Finished in 0.098s]

看起来没有文件名为最佳与.csv在同一个目录中。但文件存在。你知道吗

Files in the folder

VS代码中也存在同样的问题 似乎找不到该文件。你知道吗

但是当我在Python IDLE中运行代码时,我可以打开文件并使用它。你知道吗

有人知道怎么解决这个问题吗?你知道吗


Tags: 文件csv代码in消息mostopencall
2条回答

解决方案很简单:这里的问题是脚本的路径没有正确读入IDE。只需使用->;Atom从文件夹中打开脚本,它就能正常工作,文件也能正确加载。 谢谢大家

简单的解决方法是根据脚本所在的位置自动构建一个绝对路径

import os

path  = os.path.join(os.path.dirname(os.path.abspath(__file__)), "bestand.csv")
bestand = open(path)

相关问题 更多 >