python – FileNotFoundError: [Errno 2] No such file or directory
python – FileNotFoundError: [Errno 2] No such file or directory
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.
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.:
f = open(/Users/foo/address.csv)
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.
python – FileNotFoundError: [Errno 2] No such file or directory
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.