Python找不到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27850113/
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
Python can't find file
提问by user4434674
I am having great difficulty getting python 3.4 to recognize a path or text file on a windows 8 system. I have tried a variety of different approaches but get the similar errors (which likely implies something simple regarding the syntax).
我很难让 python 3.4 在 Windows 8 系统上识别路径或文本文件。我尝试了各种不同的方法,但得到了类似的错误(这可能暗示了一些简单的语法)。
The file itself is located in the same folder as the script file trying to open it: C:\Users\User\Desktop\Python stuff\Data.txt
该文件本身与试图打开它的脚本文件位于同一文件夹中:C:\Users\User\Desktop\Python stuff\Data.txt
for simplicity, the simplest means to access the file (at least that I know of) is
f=open
为简单起见,访问文件的最简单方法(至少我知道)是
f=open
These lines were coded as:
这些行被编码为:
f = open("Data.txt", "r")
and
和
f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
but return the error:
但返回错误:
Traceback (most recent call last):
File "C:\Users\User\Desktop\Python stuff\Testscript.py", line 3, in <module>
f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/User/Desktop/Python stuff/Data.txt'
回答by Prashant
- for
f = open("Data.txt", "r")
- 为了
f = open("Data.txt", "r")
Make sure your .py and .txt files are in the same directory.
确保您的 .py 和 .txt 文件位于同一目录中。
- for
f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
- 为了
f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
I think the space in Python stuffis messing things up.
我认为里面的空间把Python stuff事情搞砸了。
Update: I just tried this out .. seems to be fine with the space actually.
更新:我刚试过这个……实际上空间似乎没问题。
>>> [i for i in open('/Users/pk-msrb/projects/temp/temp es/temp.txt')]
['1\n', '2\n', '3\n', '\n']
回答by user7033165
When using Windows you want to keep in mind that if you name the file data.txtthe file will actually be data.txt.txt, however Windows will show it as data.txtbecause windows hides the file extensions by default. This option can be changed by following the steps in the first comment.
如果使用的是Windows,你要记住,如果你命名该文件data.txt的文件实际上是data.txt.txt,但是Windows会显示它作为data.txt,因为Windows隐藏默认的文件扩展名。可以按照第一条评论中的步骤更改此选项。
If you then search data.txtthere really is no such file.
如果你再搜索data.txt,真的没有这样的文件。
回答by Роман Белоусов
Please check your current directory.
请检查您的当前目录。
For that just do this:
为此,只需执行以下操作:
import os
print (os.getcwd())
回答by Fletchius
This is an old question, but thought I'd throw my two cents in anyway.
这是一个老问题,但我想无论如何我都会投入我的两分钱。
I had this same issue. I was using VS Code for my IDE, and had the folder setting to a folder above where I had the file. This obviously was the issue. Once I opened the folder in VScode where the code and the text file were both located I was able to open the file with no issues.
我有同样的问题。我在我的 IDE 中使用了 VS Code,并将文件夹设置为我拥有文件的文件夹。这显然是问题所在。一旦我在 VScode 中打开了代码和文本文件所在的文件夹,我就可以毫无问题地打开文件。
回答by Michal M.
The problem might be, where exactly you are executing the file.
问题可能是,您正在执行文件的确切位置。
I had the same problem, but noticed, that the terminal in my IDE (VS Code) is executing the file in a different location. By simply cdto the files location in the terminal, everything went fine. ;)
我遇到了同样的问题,但注意到我的 IDE(VS Code)中的终端正在不同的位置执行文件。通过简单地cd到终端中的文件位置,一切顺利。;)
回答by Aaron Schapman
I had the same problem but now it works for me. I used to open a big map with a lot of sub-maps and the file was in one of the sub-maps. Now I open the submap where my program and file.txt is located and the code open("file.txt", "r")works
我有同样的问题,但现在它对我有用。我曾经打开一个包含很多子地图的大地图,并且文件在其中一个子地图中。现在我打开我的程序和 file.txt 所在的子图并且代码 open("file.txt", "r")有效

