Melissa Pang
Melissa Pang

Reputation: 31

FileNotFoundError: [Errno 2] File does not exist:

I am trying to write the Python in Sublime Text. Python 3 in Mac.

Code:

import pandas as pd
df=pd.read_csv(r'\Users⁩\myxxxgmail.com⁩\Desktop\test.csv',encoding="utf-8")

Got an error:

b'\Users\xe2\x81\xa9\myxxxgmail.com\xe2\x81\xa9\Desktop\test.csv' does not exist:

File test.csv actually exists in the directory.

If I remove the "r" before directory, codes:

import pandas as pd
df=pd.read_csv('\Users⁩\myxxxgmail.com⁩\Desktop\test.csv',encoding="utf-8")

Got an error:

(unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape

Same errors if removing the session encoding="utf-8". How to fix this?

Upvotes: 2

Views: 3858

Answers (1)

Pat Jones
Pat Jones

Reputation: 896

Replace the backslashes with forward slashes on the Mac and in Linux/UNIX ('posix' operating systems). Use backslash for Windows.

If you are writing code that should be compliant with either Windows or Mac/Linux/UNIX, import the os module and check the value of os.name - then adjust your path syntax appropriately.

Upvotes: 3

Related Questions