I'm trying to load a .csv
file using the pd.read_csv()
function when I get an error despite the file path being correct and using raw strings.
import pandas as pd
df = pd.read_csv('C:\\Users\\user\\Desktop\\datafile.csv')
df = pd.read_csv(r'C:\Users\user\Desktop\datafile.csv')
df = pd.read_csv('C:/Users/user/Desktop/datafile.csv')
all gives the error below:
FileNotFoundError: File b'\xe2\x80\xaaC:/Users/user/Desktop/tutorial.csv' (or the relevant path) does not exist.
Only when i copy the file into the working directory will it load correct.
Is anyone aware of what might be causing the error?
I had previously loaded other datasets with full filepaths without any problems and I'm currently only encountering issues since I've re-installed my python (via Anaconda package installer).
Edit:
I've found the issue that was causing the problem.
When I was copying the filepath over from the file properties window, I unwittingly copied another character that seems invisible.
Assigning that copied string
also gives an unicode error.
Deleting that invisible character made any of above code work.
$10 says your file path is correct with respect to the location of the .py file, but incorrect with respect to the location from which you call python
For example, let's say script.py is located in ~/script/, and file.csv is located in ~/. Let's say script.py contains
If from ~/ you run
python script/script.py
, you will get the FileNotFound error. However, if from ~/script/ you runpython script.py
, it will work.Experienced the same issue. Path was correct. Changing the file name seems to solve the problem.
Old file name: Season 2017/2018 Premier League.csv New file name: test.csv
Possibly the whitespaces or "/"
Try using
os.path.join
to create the filepath:If you are using windows machine. Try checking the file extension. There is a high possibility of file being saved as fileName.csv.txt instead of fileName.csv You can check this by selecting File name extension checkbox under folder options (Please find screenshot)
below code worked for me: df = pd.read_csv(r"C:\Users\vj_sr\Desktop\VJS\PyLearn\DataFiles\weather_data.csv");
if fileName.csv.txt Rename/correct it as fileName.csv
windows 10 screen shot
Hope it works, Good Luck
Try this and see if it works. This is independent of the path you provide.
Here
r
is a special character and means carriage return. So prefix it to your string literal.