出于某种原因,我的代码无法打开一个简单的文件:
这是代码:
file1 = open('recentlyUpdated.yaml')
Run Code Online (Sandbox Code Playgroud)
错误是:
IOError: [Errno 2] No such file or directory: 'recentlyUpdated.yaml'
Run Code Online (Sandbox Code Playgroud)
open()的完整路径,似乎没有任何工作.这是我将其归结为简单的 open() 的代码,打开文件输入语句显示但关闭文件没有。这在空闲界面中运行,但不在命令行界面中运行。
程序和文件(拼写正确且全部小写)都在桌面上用于此测试。有没有人看到缺少什么?打开
# Read It
# Demonstrates reading from a text file
input("\n\nPress the enter key to Open file")
print("Opening and closing the file.")
text_file = open("nicole1.txt", "r")
input("\n\nPress the enter key to Close file")
text_file.close()
input("\n\nPress the enter key to exit.")
Run Code Online (Sandbox Code Playgroud)
** 更新,好的,我尝试了绝对路径,没有成功。我在闪存驱动器上有一个副本。我在 Windows XP 机器和 Windows 7 机器上运行它,它运行得很好。我使用相同的闪存驱动器并尝试在 Windows10 Box 上运行它,但我遇到了问题。一条评论询问是否有回溯,并且有,它基本上表明该文件不存在。我现在正在尝试确定这是否是 Windows 10 问题。此外,代码将在两个 Windows Box(XP 和 Win10)上的 idle 内运行。
当我使用绝对路径时,代码工作正常,但使用相对路径会在 python 中抛出 FileNotFoundError 。
f = open("Input.txt","r")
Run Code Online (Sandbox Code Playgroud)