在 Atom 中打开文件时出现问题,VS 代码仅在 Python IDLE 中工作

Problem with open files in Atom, VS Code only working in Python IDLE

我的问题: 我想使用名为 bestand.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]

在同一目录中似乎没有名为 bestand.csv 的文件。但是文件存在。

同样的问题在 VS-Code 中 似乎找不到该文件。

但是当我 运行 代码处于 Python IDLE 时,我可以打开文件并使用它。

有人知道如何解决这个问题吗?

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

import os

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

解决方法和想的一样简单:这里的问题是脚本的路径没有被正确读入IDE。只需使用 -> Atom 从文件夹中打开脚本,它就可以工作并且文件已正确加载。 谢谢大家