5

Currently I am trying to read in a csv file using the csv module in python. When I run the piece of code below I get an error that states the file does not exist. My first guess is that maybe, I have the file saved in the wrong place or I need to provide pyton with a file path. currently I have the file saved in C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv.

one side note im note sure wether having set the mode to 'rb' (read binary) was the right move.

import csv
with open('test_satdata.csv', 'rb') as csvfile:
    satreader = csv.reader(csvfile, delimiter=' ', lineterminator=" ")
    for row in satreader:
        print ', '.join(row)

Here is the errror code.

Traceback (most recent call last):
File "C:/Python27/test code/test csv parse.py", line 2, in <module>
    with open('test_satdata.csv', 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: 'test_satdata.csv'
0

4 Answers 4

7

Your code is using a relative path; python is looking in the current directory (whatever that may be) to load your file. What the current directory is depends on how you started your Python script and if you executed any code that may have changed the current working directory.

Use a full absolute path instead:

path = r'C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv'
with open(path, 'rb') as csvfile:

Using 'rb' is entirely correct, the csv module recommends you do so:

If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference.

Windows is such a platform.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I did exaclty what you recomended and I got python to output the file.
2

You can hit this error when you're running a Python script from a Directory where the file is not contained.

Sounds simple to fix, put the CSV file in the same folder as the .PY file. However when you're running under an IDE like VSCode the command output might cd to another directory when it executes your python file.

PS C:\git\awesome> cd 'c:\git\awesome'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; 
& 'C:\Program Files (x86)\Python37-32\python.exe' 'c:\Users\jeremy\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\ptvsd_launcher.py' 
'--default' '--client' '--host' 'localhost' '--port' '1089' 
'c:\git\Project\ReadCSV.py'

See how I'm in my awesome repo and the first command is: cd 'c:\git\awesome';

Then it executes the python file: 'c:\git\Project\ReadCSV.py'

So its expecting the CSV file in 'c:\git\awesome'.

To fix it, either use the full file names or CD to the directory containing the CSV file you wish to read.

Alternatively in VSCode you can fix this for Python: Go to Preferences > settings. Type "Execute" on search bar. Select 'Python' under 'Extensions'. Check the checkbox 'Whether to use the directory of the file to be executed as the working directory

enter image description here

1 Comment

For VSCode you can fix this : Go to Preferences > settings . Type "Execute" on search bar. Select 'Python' under 'Extensions'. Check the checkbox 'Whether to use the directory of the file to be executed as the working directory'.
0

Your current guess is right: either put the file in your test code directory or point python to the right path.

2 Comments

No, 'rb' is entirely correct for opening a CSV file for the csv module.
I just saw that on the csv module reference page: ". If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference" .. my apologies for not noticing the import csv line. Edited my response.
-3

Make a fresh rename of your folder. That worked for me.

1 Comment

This is very uninformative. I don't think this qualifies as an answer. Either elaborate, or delete would be my suggestion

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.