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:46

There is an another problem on how to delete the characters that seem invisible.

My solution is copying the filepath from the file windows instead of the property windows.

That is no problem except that you should fulfill the filepath.

查看更多
等我变得足够好
3楼-- · 2019-04-23 04:50

I know following is a silly mistake but it could be the problem with your file.

I've renamed the file manually from adfa123 to abc.csv. The extension of the file was hidden, after renaming, Actual File name became abc.csv.csv. I've then removed the extra .csv from the name and everything was fine.

Hope it could help anyone else.

查看更多
Fickle 薄情
4楼-- · 2019-04-23 04:51

I was trying to read the csv file from the folder that was in my 'c:\'drive but, it raises the error of escape,type error, unicode......as such but this code works just take an variable then add r to read it.

rank = pd.read_csv (r'C:\Users\DELL\Desktop\datasets\iris.csv') 
df=pd.DataFrame(rank)
查看更多
登录 后发表回答