小编Thi*_*Guy的帖子

FileNotFoundError:[Errno 2]

概要:如何在Python中读取文件?为什么一定要这样做?

我的问题是我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\Terminal\Desktop\wkspc\filetesting.py", line 1, in <module>
    testFile=open("test.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
Run Code Online (Sandbox Code Playgroud)

其源自以下代码:(即整个'.py'文件)

testFile=open("test.txt")
print(testFile.read())
Run Code Online (Sandbox Code Playgroud)

"test.txt"与我的程序位于同一文件夹中.我是Python的新手,不明白为什么我会收到文件位置错误.我想知道修复以及必须以这种方式完成修复的原因.

我试过使用文件的绝对路径,"C:\ Users\Terminal\Desktop\wkspc\test.txt"

其他详情:

"Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32"
Windows 7, 32 Bit
Run Code Online (Sandbox Code Playgroud)

python file-io python-3.x

3
推荐指数
1
解决办法
3万
查看次数

标签 统计

file-io ×1

python ×1

python-3.x ×1