SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

avatar

Borislav Hadzhiev

Last updated: May 1, 2022

banner

Photo from Unsplash

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape #

The Python "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position" occurs when we have an unescaped backslash character in a path. To solve the error, prefix the path with r to mark it as a raw string, e.g. r'C:\Users\Bob\Desktop\example.txt'.

syntaxerror unicode error unicdeescape codec cant decode bytes

Here is an example of how the error occurs.

main.py
# ⛔️ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape file_name = 'C:\Users\Bob\Desktop\example.txt' with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

The path contains backslash characters which is the cause of the error.

The backslash \ character has a special meaning in Python - it is used as an escape character (e.g. \n or \t).

One way to solve the error is to prefix the string with the letter r to mark it as a raw string.

main.py
# ✅ prefix string with r file_name = r'C:\Users\Bob\Desktop\example.txt' with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

An alternative way to treat a backslash \ as a literal character is to escape it with a second backslash \\.

main.py
# ✅ escape each backslash with a second backslash file_name = 'C:\\Users\\Bob\\Desktop\\example.txt' with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

We escaped each backslash character to treat them as literal backslashes.

Here is a string that shows how 2 backslashes only get translated into 1.

main.py
my_str = 'hello\\world' print(my_str) # 👉️ "hello\world"

Similarly, if you need to have 2 backslashes next to one another, you would have to use 4 backslashes.

main.py
my_str = 'hello\\\\world' print(my_str) # 👉️ "hello\\world"

An alternative solution to the "unicodeescape codec can't decode bytes in position" error is to use forward slashes in the path instead of backslashes.

main.py
# ✅ using forward slashes instead of backslashes file_name = 'C:/Users/Bob/Desktop/example.txt' with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

A forward slash can be used in the place of a backslash when you need to specify a path.

This solves the error because we no longer have any unescaped backslash characters in the path.

Since backslash characters have a special meaning in Python, we need to treat them as a literal character by:

  • prefixing the string with r to mark it as a raw string
  • escaping each backslash with a second backslash
  • using forward slashes in the place of backslashes in the path

Conclusion #

The Python "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position" occurs when we have an unescaped backslash character in a path. To solve the error, prefix the path with r to mark it as a raw string, e.g. r'C:\Users\Bob\Desktop\example.txt'.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.