130

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
embert
  • 7,336
  • 10
  • 49
  • 78
user3266816
  • 1,301
  • 2
  • 9
  • 3
  • 5
    Is `address.csv` in the same folder as your python (.py) code? You are using a relative path. If it's not you need to provide a full path like `C:\folder\folder\address.csv` – Cory Kramer Mar 09 '14 at 13:29
  • if you could post the exact error, it would be easy to answer. – Srivatsan Mar 09 '14 at 13:33
  • 4
    @Cyber The file is not relative to the dir containing the source code. It is relative to the working directory. By coincidence those directories may be the same, but it's important to be precise. – David Heffernan Mar 09 '14 at 13:35
  • I forgot to write permission, after that, I add "wb" and working properly. thanks – Saiful Islam Dec 28 '19 at 02:03

6 Answers6

149

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")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
tsroten
  • 2,534
  • 1
  • 14
  • 17
  • great answer! I have a folder and then some folders inside it and it turns out that the file I wanted to read should be inside the first folder, not the others, and I could only figure that out because of your comment – Carlos Junior Jun 02 '22 at 16:48
36

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.

tylerswright
  • 67
  • 2
  • 12
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    okay that works but what if you want to distribute the code so people can use it? you cant expect absolute path to work in that scenario right? – Abhishta Gatya Oct 08 '17 at 15:31
  • 2
    @AbhishtaGatya, in that case you have to setup environment variables to read the present working directory! – Dr. Essen Aug 09 '18 at 07:36
20

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.

Rajat Soni
  • 600
  • 7
  • 10
  • I have brackets "()" in my file path as well as dashes "-". Would these also throw it off? I cannot change the nature however of the file path because it belongs to an organization. any thoughts? – Max Duso Aug 28 '23 at 16:54
8
with open(fpath, 'rb') as myfile:
    fstr = myfile.read()

I encounter this error because the file is empty. This answer may not be a correct answer for this question but hopefully it can give some of you a hint.

Bowen Xu
  • 3,836
  • 1
  • 23
  • 25
1

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)
huck dupr
  • 36
  • 5
Dinesh Reddy
  • 775
  • 1
  • 11
  • 25
  • 5
    Are you saying that absolute paths should always be used and relative paths should not be used? – David Heffernan Mar 09 '14 at 13:57
  • 1
    NO. I am saying it is better to use absolute path. If the script and the input file both are in same directory then relative should work. – Dinesh Reddy Mar 10 '14 at 12:44
  • 1
    It is not better if you want the path to be relative. What's more, it is relative to the **working directory** and the script directory. – David Heffernan Mar 10 '14 at 12:50
1

Lets say we have a script in "c:\script.py" that contain :

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)

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

C:\python script.py

'<html><head></head><body></body></html>'

This is why is it preferable to use absolute path.

Aouffen
  • 19
  • 1