'IOError: [Errno 2] No such file or directory: 'sample.csv'
I am getting an IOError:
[Errno 2] No such file or directory: 'sample.csv'
The CSV file exists in the same location as the script. I am running it from the same path as well. This is my code:
import csvkit
file_name='sample.csv'
with open(file_name,'rb') as f:
reader = csvkit.reader(f)
print reader
Solution 1:[1]
You need the entire path...
import csvkit
file_name='users/zinedine/documents/sample.csv' # Must be full path
with open(file_name,'rb') as f:
reader = csvkit.reader(f)
print reader
Or...
Set the current working directory:
os.chdir("My/directory")
And continue on like normal.
Solution 2:[2]
You need the full path to the file:
import csvkit
file_name='/path/to/sample.csv'
with open(file_name,'rb') as f:
reader = csvkit.reader(f)
print reader
Or if you don't know the full path and want more portability out of your program, you could get the full path like this (assuming the script and file are in the same directory and the files name is sample.csv):
import csvkit
import os
filePath = [os.path.realpath(os.path.join('.',f)) for f in os.listdir('.') if os.path.isfile(f) and f == 'sample.csv'][0]
with open(filePath,'rb') as f:
reader = csvkit.reader(f)
print reader
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zizouz212 |
| Solution 2 |
