Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundError: [Errno 2] No such file or directory [duplicate]

Tags:

python

file

find

I am trying to open a CSV file but for some reason python cannot locate it.

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 
like image 316
user3266816 Avatar asked Mar 09 '14 13:03

user3266816


People also ask

How do I fix FileNotFoundError Errno 2 No such file or directory?

The error "FileNotFoundError: [Errno 2] No such file or directory" is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path. In the above code, all of the information needed to locate the file is contained in the path string - absolute path.

What does FileNotFoundError Errno 2 No such file or directory mean?

The error FileNotFoundError Errno 2 no such file or directory occurs when Python cannot find the specified file in the current directory.

How do you fix error number 2 in Python?

The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.


1 Answers

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") 
like image 102
tsroten Avatar answered Sep 29 '22 04:09

tsroten