我在一个简单的 Python open() 函数上收到 File not Found 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33883840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:09:07  来源:igfitidea点击:

I am getting a File not Found error on a simple Python open() function

pythonfilewindows-10

提问by Nicole Cook

Here is the code I have boiled it down to a simple open(), the Open file input statement displays but the Close file does not. This runs in the Idle interface but not in the command line interface.

这是我将其归结为简单的 open() 的代码,打开文件输入语句显示但关闭文件没有。这在空闲界面中运行,但不在命令行界面中运行。

Both the program and the file (spelled correctly and all lower case) are on the desktop for this test. Does anyone see what is missing?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.")

** Update, Ok I tried the absolute path and it was not successful. I have a copy of this on a flash drive. I ran it on a Windows XP box and a Windows 7 box and it ran just fine. I take the same flash drive and try and run it on a Windows10 Box and I get the problem. One comment asked if there was a traceback and there is and it basically indicates that the file does not exist. I am now trying to determine if this is a Windows 10 issue. Also, the code will run inside idle on both Windows Boxes (XP and Win10).

** 更新,好的,我尝试了绝对路径,没有成功。我在闪存驱动器上有一个副本。我在 Windows XP 机器和 Windows 7 机器上运行它,它运行得很好。我使用相同的闪存驱动器并尝试在 Windows10 Box 上运行它,但我遇到了问题。一条评论询问是否有回溯,并且有,它基本上表明该文件不存在。我现在正在尝试确定这是否是 Windows 10 问题。此外,代码将在两个 Windows Box(XP 和 Win10)上的 idle 内运行。

回答by Jazib

How are you running this program? If by command line, then put the file into same folder where the python file that contains this code is situated. As others have pointed out, the issue is program is unable to find the file. If you're running this via some IDE, then set working directory to this path. For example, Pycharm (and other Intellij IDEs) have this in "Edit Configuration".

你是如何运行这个程序的?如果通过命令行,则将文件放入包含此代码的 python 文件所在的同一文件夹中。正如其他人所指出的,问题是程序无法找到该文件。如果您通过某个 IDE 运行它,则将工作目录设置为此路径。例如,Pycharm(和其他 Intellij IDE)在“编辑配置”中有这个。

回答by 1313e

Well, besides the fact that I named it nicole1.txt.txtby accident initially (due to the way that Windows automatically uses extensions and Linux does not), it works perfectly fine for me using Windows 10 Home and Pro, and Ubuntu 16.04. I simply just executed python test.pyin the command prompt with test.pycontaining your script and making sure both files were in the same directory.

好吧,除了我nicole1.txt.txt最初偶然命名的事实(由于 Windows 自动使用扩展的方式而 Linux 没有),它对我使用 Windows 10 家庭版和专业版以及 Ubuntu 16.04 来说非常好。我只是python test.py在命令提示符下执行,其中test.py包含您的脚本并确保两个文件都在同一目录中。

I cannot test it on any other Windows version, as I no longer use them.

我无法在任何其他 Windows 版本上测试它,因为我不再使用它们。

Also, btw, you may want to rewrite your code to:

另外,顺便说一句,您可能希望将代码重写为:

input("\n\nPress the enter key to Open file")
print("Opening and closing the file.")
with open("nicole1.txt", "r") as text_file:
    input("\n\nPress the enter key to Close file")
    text_file.close()
input("\n\nPress the enter key to exit.")

This way, you make sure the file is closed no matter what happens. And yes, I know, technically speaking the file is closed twice.

这样,无论发生什么,您都可以确保文件已关闭。是的,我知道,从技术上讲,该文件已关闭两次。