13

In Python, I have a script, I'm trying to use the python open("data.csv") command to open a CSV file that I have in the Python script directory.

There is a file there called data.csv.

The python script indicates an error:

Error in Python IOError: [Errno 2] No such file or directory: 'data.csv'

What does this error mean and how do I fix it?

Here is the minimal code in the script that reproduces the error:

open("data.csv")
Chris
  • 4,009
  • 3
  • 21
  • 52
Doug Fir
  • 19,971
  • 47
  • 169
  • 299

4 Answers4

18

Try to give the full path to your csv file

open('/users/gcameron/Desktop/map/data.csv')

The python process is looking for file in the directory it is running from.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
9

open looks in the current working directory, which in your case is ~, since you are calling your script from the ~ directory.

You can fix the problem by either

  • cding to the directory containing data.csv before executing the script, or

  • by using the full path to data.csv in your script, or

  • by calling os.chdir(...) to change the current working directory from within your script. Note that all subsequent commands that use the current working directory (e.g. open and os.listdir) may be affected by this.
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 2
    os.chdir() should be used with care or better yet not at all in this case (os.path.join could be used instead). – jfs Oct 21 '12 at 17:55
2

You need to either provide the absolute path to data.csv, or run your script in the same directory as data.csv.

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38
2

It's looking for the file in the current directory.

First, go to that directory

cd /users/gcameron/Desktop/map

And then try to run it

python colorize_svg.py
Eric
  • 3,142
  • 17
  • 14