pandas.read_csv file not found despite correct pat

2019-04-23 03:52发布

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.

9条回答
成全新的幸福
2楼-- · 2019-04-23 04:25

$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

import pandas
df = pandas.read_csv('../file.csv') # correct path from ~/script/ where script.py resides

If from ~/ you run python script/script.py, you will get the FileNotFound error. However, if from ~/script/ you run python script.py, it will work.

查看更多
可以哭但决不认输i
3楼-- · 2019-04-23 04:26

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 "/"

查看更多
贪生不怕死
4楼-- · 2019-04-23 04:29

Try using os.path.join to create the filepath:

import os
f_path = os.path.join(*['C:', 'Users', 'user', 'Desktop', 'datafile.csv'])
df = pd.read_csv(f_path)
查看更多
看我几分像从前
5楼-- · 2019-04-23 04:36
import pandas as pd

path1 = 'C:\\Users\\Dell\\Desktop\\Data\\Train_SU63ISt.csv'
path2 = 'C:\\Users\\Dell\\Desktop\\Data\\Test_0qrQsBZ.csv'

df1 = pd.read_csv(path1)
df2 = pd.read_csv(path2)

print(df1)
print(df2)
查看更多
萌系小妹纸
6楼-- · 2019-04-23 04:39

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

查看更多
相关推荐>>
7楼-- · 2019-04-23 04:41

Try this and see if it works. This is independent of the path you provide.

pd.read_csv(r'C:\Users\aiLab\Desktop\example.csv')

Here r is a special character and means carriage return. So prefix it to your string literal.

查看更多
登录 后发表回答