【发布时间】:2022-01-07 12:20:35
【问题描述】:
我正在关注有关文件的 python 教程,但我无法在与 python 脚本相同的目录中打开文本文件。这有什么原因吗?
f = open("test.txt", "r")
print(f.name)
f.close()
错误信息:
Traceback (most recent call last):
File "c:\Users\07gas\OneDrive\Documents\pyFileTest\ManipulatingFiles.py", line 1, in <module>
f = open("test.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
Here's a screenshot of proof of it being in the same directory:
【问题讨论】:
-
你是从同一个目录执行的吗?
-
它告诉你
test.txt不在当前工作目录中。我们不知道那是什么。你是如何运行代码的,环境选项有哪些? -
他们在同一个文件夹中查看屏幕截图
-
你是如何运行python脚本的?
-
修复错字后出现此错误:
File "c:\Users\07gas\OneDrive\Documents\pyFileTest\ManipulatingFiles.py", line 3, in <module> f = open(os.path.abspath("test.txt"), "r") FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\07gas\\test.txt'