error "no such file or directory" when reading in csv file in python [duplicate]
Asked Answered
T

4

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'
Torpor answered 5/8, 2013 at 16:9 Comment(0)
E
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.

Epidiascope answered 5/8, 2013 at 16:11 Comment(1)
Thank you, I did exaclty what you recomended and I got python to output the file.Torpor
T
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.

Torch answered 1/10, 2019 at 2:20 Comment(0)
K
0

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

Kataway answered 5/8, 2013 at 16:10 Comment(2)
No, 'rb' is entirely correct for opening a CSV file for the csv module.Epidiascope
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.Kataway
H
-3

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

Heinrick answered 15/1, 2016 at 21:57 Comment(1)
This is very uninformative. I don't think this qualifies as an answer. Either elaborate, or delete would be my suggestionHoman

© 2022 - 2024 — McMap. All rights reserved.