“Unicode Error “unicodeescape” codec can’t decode bytes… Cannot open text files in Python 3

I am using Python 3.1 on a Windows 7 machine. Russian is the default system language, and utf-8 is the default encoding.

Looking at the answer to a previous question, I have attempting using the “codecs” module to give me a little luck. Here’s a few examples:

>>> g = codecs.open("C:UsersEricDesktopbeeline.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated UXXXXXXXX escape (<pyshell#39>, line 1)
>>> g = codecs.open("C:UsersEricDesktopSite.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated UXXXXXXXX escape (<pyshell#40>, line 1)
>>> g = codecs.open("C:Python31Notes.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 11-12: malformed N character escape (<pyshell#41>, line 1)
>>> g = codecs.open("C:UsersEricDesktopSite.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated UXXXXXXXX escape (<pyshell#44>, line 1)

My last idea was, I thought it might have been the fact that Windows “translates” a few folders, such as the “users” folder, into Russian (though typing “users” is still the correct path), so I tried it in the Python31 folder. Still, no luck. Any ideas?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The problem is with the string

"C:UsersEricDesktopbeeline.txt"

Here, U in "C:Users… starts an eight-character Unicode escape, such as U00014321. In your code, the escape is followed by the character ‘s’, which is invalid.

You either need to duplicate all backslashes:

"C:\Users\Eric\Desktop\beeline.txt"

Or prefix the string with r (to produce a raw string):

r"C:UsersEricDesktopbeeline.txt"

Method 2

Typical error on Windows because the default user directory is C:user<your_user>, so when you want to pass this path as a string argument into a Python function, you get a Unicode error, just because the u is a Unicode escape. If the next 8 characters after the u are not numeric this produces an error.

To solve it, just double the backslashes: C:\user\<your_user>...
This will ensure that Python treats the single backslashes as single backslashes.

Method 3

Prefixing with 'r' works very well, but it needs to be in the correct syntax. For example:

passwordFile = open(r'''C:UsersBobSecretPasswordFile.txt''')

No need for \ here – maintains readability and works well.

Method 4

With Python 3 I had this problem:

 self.path = 'T:PythonScriptsProjectsUtilities'

produced this error:

 self.path = 'T:PythonScriptsProjectsUtilities'
            ^
 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
 position 25-26: truncated UXXXXXXXX escape

the fix that worked is:

 self.path = r'T:PythonScriptsProjectsUtilities'

It seems the ‘U’ was producing an error and the ‘r’ preceding the string turns off the eight-character Unicode escape (for a raw string) which was failing. (This is a bit of an over-simplification, but it works if you don’t care about unicode)

Hope this helps someone

Method 5

Or you could replace ‘’ with ‘/’ in the path.

Method 6

path = pd.read_csv(**'C:UsersmraviDesktopfilename'**)

The error is because of the path that is mentioned

Add 'r' before the path

path = pd.read_csv(**r'C:UsersmraviDesktopfilename'**)

This would work fine.

Method 7

I had this same error in python 3.2.

I have script for email sending and:

csv.reader(open('work_diruslugi1.csv', newline='', encoding='utf-8'))

when I remove first char in file uslugi1.csv works fine.

Method 8

Refer to openpyxl document, you can do changes as followings.

from openpyxl import Workbook
from openpyxl.drawing.image import Image

wb = Workbook()
ws = wb.active
ws['A1'] = 'Insert a xxx.PNG'
# Reload an image
img = Image(**r**'x:xxxxxxxxx.png')
# Insert to worksheet and anchor next to cells
ws.add_image(img, 'A2')
wb.save(**r**'x:xxxxxx.xlsx')

Method 9

I had same error, just uninstalled and installed again the numpy package, that worked!

Method 10

I had this error.
I have a main python script which calls in functions from another, 2nd, python script.
At the end of the first script I had a comment block designated with ''' '''.
I was getting this error because of this commenting code block.
I repeated the error multiple times once I found it to ensure this was the error, & it was.
I am still unsure why.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x