Python FileNotFoundError: [Errno 2] 没有那个文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22282760/
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
FileNotFoundError: [Errno 2] No such file or directory
提问by user3266816
I am trying to open a CSV file but for some reason python cannot locate it.
我正在尝试打开一个 CSV 文件,但由于某种原因 python 无法找到它。
Here is my code (it's just a simple code but I cannot solve the problem):
这是我的代码(它只是一个简单的代码,但我无法解决问题):
import csv
with open('address.csv','r') as f:
reader = csv.reader(f)
for row in reader:
print row
回答by David Heffernan
You are using a relative path, which means that the program looks for the file in the working directory. The error is telling you that there is no file of that name in the working directory.
您使用的是相对路径,这意味着程序会在工作目录中查找文件。该错误告诉您工作目录中没有该名称的文件。
Try using the exact, or absolute, path.
尝试使用确切的或绝对的路径。
回答by tsroten
When you open a file with the name address.csv, you are telling the open()function that your file is in the current working directory. This is called a relative path.
当您打开一个名为 的文件时address.csv,您是在告诉open()函数您的文件在当前工作目录中。这称为相对路径。
To give you an idea of what that means, add this to your code:
为了让您了解这意味着什么,请将其添加到您的代码中:
import os
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directory
print("Files in %r: %s" % (cwd, files))
That will print the current working directory along with all the files in it.
这将打印当前工作目录及其中的所有文件。
Another way to tell the open()function where your file is located is by using an absolute path, e.g.:
另一种告诉open()函数文件所在位置的方法是使用绝对路径,例如:
f = open("/Users/foo/address.csv")
回答by Dinesh
Use the exact path.
使用确切的路径。
import csv
with open('C:\path\address.csv','r') as f:
reader = csv.reader(f)
for row in reader:
print row
回答by Aouffen
Lets say we have a script in "c:\script.py" that contain :
假设我们在“c:\script.py”中有一个包含以下内容的脚本:
result = open("index.html","r")
print(result.read())
Lets say that the index.html file is also in the same directory "c:\index.html" when i execute the script from cmd (or shell)
假设当我从 cmd(或 shell)执行脚本时,index.html 文件也在同一目录“c:\index.html”中
C:\Users\Amine>python c:\script.py
You will get error:
你会得到错误:
FileNotFoundError: [Errno 2] No such file or directory: 'index.html'
And that because "index.html" is not in working directory which is "C:\Users\Amine>". so in order to make it work you have to change the working directory
那是因为“index.html”不在“C:\Users\Amine>”的工作目录中。所以为了让它工作,你必须改变工作目录
C:\python script.py
'<html><head></head><body></body></html>'
This is why is it preferable to use absolute path.
这就是为什么最好使用绝对路径的原因。
回答by Rajat Soni
For people who are still getting error despite of passing absolute path, should check that if file has a valid name. For me I was trying to create a file with '/' in the file name. As soon as I removed '/', I was able to create the file.
对于尽管传递了绝对路径但仍然出现错误的人,应该检查文件是否具有有效名称。对我来说,我试图创建一个文件名中带有“/”的文件。删除“/”后,我就能够创建文件。
回答by Brad
with open(fpath, 'rb') as myfile:
fstr = myfile.read()
I encounter this error because the file is empty. This answer may not a correct answer for this question but should give developers a hint like me.
我遇到此错误是因为文件为空。这个答案可能不是这个问题的正确答案,但应该给像我这样的开发人员一个提示。

