简体   繁体   English

在Atom中打开文件的问题,VS Code仅在Python IDLE中有效

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

My Problem: I want to work with a file called bestand.csv. 我的问题:我想使用一个名为bestand.csv的文件。 So I have written some code to open the file: 所以我写了一些代码打开文件:

bestand = open("bestand.csv")

When I want to run the code in Atom then I get this message: 当我想在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]

Looks like there is no file called bestand.csv in the same directory. 看起来在同一目录中没有名为bestand.csv的文件。 But the file exist. 但是文件存在。

文件夹中的文件

The same problem is in VS-Code The file does not appear to be found. VS-Code中存在相同问题。似乎找不到该文件。

But when I run the code in Python IDLE I can open the file and can work with it. 但是,当我在Python IDLE中运行代码时,我可以打开文件并可以使用它。

Does anyone have any idea how to fix this? 有谁知道如何解决这个问题?

Easy fix could be automatically building an absolute path by where your script is located 轻松修复可能会根据脚本的位置自动建立绝对路径

import os

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

The solution is simple as thought: The problem here is that the path of the script was not read correctly into the IDE. 解决方案很简单:此处的问题是脚本的路径未正确读取到IDE中。 Simply open the script from the folder with -> Atom and it works and the files are loaded correctly. 只需使用-> Atom从文件夹中打开脚本即可,脚本可以正常工作并且文件已正确加载。 Thank you all 谢谢你们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM