【发布时间】:2019-11-24 19:38:44
【问题描述】:
所以我正在编写需要导入文件的代码。 python代码文件和我需要导入的文件都在同一个目录中,所以我没有指定整个路径。
代码在 IDLE 中运行良好,但在 Visual Studio Code 中出现错误
我添加了行 “打印(少数)” 检查它是否在 IDLE 中工作。它会打印出来。
import random
infile=open("StatesANC.txt","r")
print("few")
我在 Visual Studio 中得到的错误如下:-
Traceback (most recent call last):
File "f:/SKKU/study/ISS3178 - Python/11/Lab Assignment 11.py", line 2, in <module>
infile=open("StatesANC.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'StatesANC.txt'
【问题讨论】:
-
import os后跟os.getcwd()返回什么? -
IDLE 和 VS Code 不在同一个文件夹中运行...无论如何打开相对于脚本 stackoverflow.com/questions/1270951/… 的文件会更好
-
我真的不明白你想让我把那个代码放在哪里,以及如何将它跟进到第二部分,因为我对 python 还很陌生。因此,这是根据您的建议对代码进行更改后的代码样子 -
import os os.getcwd() import random infile=open("StatesANC.txt","r") print("few")IDLE 的输出没有变化,VS Code 的错误也没有变化。
标签: python python-3.x visual-studio-code python-idle