21

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.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Impuls3H
  • 303
  • 1
  • 2
  • 11
  • 5
    `e2 80 aa` is the UTF-8 encoding of U+202A, the left-to-right embedding symbol. Pretty sure this doesn't belong in a path string, so it suggests that you've got a string encoding issue. Try Unicode string literals if you're in py27 (`u'C:\\...'`) or byte string literals if you're in py3 (`b'C:\\...'`). – wildwilhelm Feb 10 '17 at 17:56
  • 1
    Does it work if you remove the drive from the path (i.e. '/Users/user/Desktop/datafile.csv')? – cmaher Feb 10 '17 at 17:57
  • 2
    Not sure how `U+202A` got in there. Was this cut/pasted? If so, delete and reenter manually. What is the default language on your system? If its written right-to-left then this is interesting! I'm a bit surprised that the character isn't filtered out at the file system. – tdelaney Feb 10 '17 at 18:13
  • 1
    It's most probable cause seems to be a problem with the encoding you are using. – Anwar Shaikh Feb 10 '17 at 20:44
  • I'm using Python 3.6, the byte string literals didn't seem to work. Removing the drive from the path as per cmaher suggestion works! I'm wondering if I have multiple drive, how should i state the other drives? – Impuls3H Feb 13 '17 at 12:36
  • 2
    I've just had this same issue. It seems the extra character comes in when I coy the path of the file from the security tab of the file properties in windows. Anyone know what this non-unicode character is? – Jamie Marshall Oct 10 '18 at 16:50
  • I think its as per @wildwilhelm comment, its the UTF-8 encoding of U+202A, left to right embedding symbol. – Impuls3H Nov 29 '18 at 13:03
  • @OP: How did you go about 'Deleting that invisible character '? – Victor Feb 06 '19 at 17:52
  • As simple as hitting the delete/backspace button in my case. You might want to strip any whitespace if you're having issues with that – Impuls3H Feb 20 '19 at 07:38

14 Answers14

31

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 raw string. So prefix it to your string literal.

https://www.journaldev.com/23598/python-raw-string:

Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python raw string treats backslash () as a literal character. This is useful when we want to have a string that contains backslash and don’t want it to be treated as an escape character.

Alex Martian
  • 3,423
  • 7
  • 36
  • 71
WaterRocket8236
  • 1,442
  • 1
  • 17
  • 27
12

$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.

hertopnerd
  • 618
  • 1
  • 5
  • 9
3

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.

Waqar Khan
  • 468
  • 4
  • 18
3
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)
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
3

On Windows systems you should try with os.path.normcase.

It normalize the case of a pathname. On Unix and Mac OS X, this returns the path unchanged; on case-insensitive filesystems, it converts the path to lowercase. On Windows, it also converts forward slashes to backward slashes. Raise a TypeError if the type of path is not str or bytes (directly or indirectly through the os.PathLike interface).

import os
import pandas as pd

script_dir = os.getcwd()
file = 'example_file.csv'
data = pd.read_csv(os.path.normcase(os.path.join(script_dir, file)))
Radmar
  • 73
  • 7
2

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:

import pandas as pd
df = pd.read_csv(r"C:\Users\vj_sr\Desktop\VJS\PyLearn\DataFiles\weather_data.csv");

If fileName.csv.txt, rename/correct it to fileName.csv

windows 10 screen shot

Hope it works, Good Luck

vj sreenivasan
  • 1,283
  • 13
  • 15
1

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)
Alex
  • 12,078
  • 6
  • 64
  • 74
  • 1
    I tried to create the filepath as per your suggestion but it didn't work out. The error "FileNotFoundError: File b'C:Users\\user\\Desktop\\tutorial.csv' does not exist" comes up when I tried to combine it. Removing the drive as per cmaher's suggestion worked, but I'm wondering in future what should I type if I need to state the drive? – Impuls3H Feb 13 '17 at 12:38
  • 1
    search for your problem on google or SO and you will find a solution – Alex Feb 13 '17 at 17:38
  • 1
    I managed to identify the issue that I was facing. It was an invisible character that was copied from the filepath found in the file properties that messed up the filepath. Removing that invisible character worked after all. – Impuls3H Mar 07 '17 at 16:20
1

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)
Rakesh
  • 11
  • 2
  • 1
    i hv done it its wrking even that sloution is true but adding "r" to it made the difference – Rakesh Aug 30 '18 at 18:32
1

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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Dee Lee
  • 21
  • 2
  • 1
    I had the same problem on mine, for whatever reason if you copy from the properties window it adds extra invisible characters. I was able to verify this because if I type the path in manually, it works fine. It's by far one of the most strange errors I've come across in a while. – areed1192 Mar 19 '19 at 02:49
1

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

1

I had the same problem when running the file with the interactive functionality provided by Visual studio. Switched to running on the native command line and it worked for me.

Patrick Mutuku
  • 1,095
  • 15
  • 13
1

For my particular issue, the failure to load the file correctly was due to an "invisible" character that was introduced when I copied the filepath from the security tab of the file properties in windows.

This character is e2 80 aa, the UTF-8 encoding of U+202A, the left-to-right embedding symbol. It can be easily removed by erasing (hitting backspace or delete) when you've located the character (leftmost char in the string).

Note: I chose to answer because the answers here do not answer my question and I believe a few folks (as seen in the comments) might meet the same situation as I did. There also seems to be new answers every now and then since I did not mark this question as resolved.

Impuls3H
  • 303
  • 1
  • 2
  • 11
0

I had similar problem when I was using JupyterLab + Anaconda, and used my browser to type stuff on IDE.

My problem was simpler - there was a very subtle typo error. I didn't have to use raw text - either escaping or using {{r}} string worked for me :). If you use Anaconda with Jupyter Lab, I believe the Kernel starts with where you open the notebook from i.e. the working directory is that top level folder.

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
-1
data = pd.read_csv('C:\\Users\username\Python\mydata.csv')

This worked for me. Note the double "\\" in "C:\\" where the rest of the folders use only a single "\".