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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# 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

shell
File "/home/borislav/Desktop/bobbyhadz_python/main.py", line 2 file_name = 'C:\Users\Bob\Desktop\example.txt' ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

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)

string contains backslash characters

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

# Prefix the string with r to mark it as a raw string

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.

If the error persists, try to use a triple-quoted raw string instead.

main.py
# ✅ Wrapped raw string in triple quotes file_name = r'''C:\Users\Bob\Desktop\example.txt''' with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

You might also use the open() function directly, without the with statement.

main.py
file_name = r'C:\Users\Bob\Desktop\example.txt' my_file = open(file_name, 'r', encoding='utf-8') lines = my_file.readlines() print(lines) my_file.close()

Prefixing the string with r works either way.

# Escape the backslash with a second backslash character

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 = 'bobby\\hadz' print(my_str) # 👉️ "bobby\hadz"

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

main.py
my_str = 'bobby\\\\hadz\\\\com' print(my_str) # 👉️ "bobby\\hadz\\com"

# Using forward slashes instead of backslashes in paths

An alternative solution to the 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 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.

The error occurs because the \U character in the path is a Unicode code point.

main.py
file_name = 'C:\Users\Bob\Desktop\example.txt'

If the 8 characters after \U are not numeric an error is raised.

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 place of backslashes in the path

# The 3 possible solutions to the error

Here are the 3 possible solutions to the error.

main.py
# ✅ Prefix string with r file_name = r'C:\Users\Bob\Desktop\example.txt' # ✅ Escaping each backslash with another backslash file_name = 'C:\\Users\\Bob\\Desktop\\example.txt' # ✅ Using forward slashes instead of backslashes file_name = 'C:/Users/Bob/Desktop/example.txt'

If none of the suggestions works, try to use a triple-quoted raw string.

main.py
file_name = r'''C:\Users\Bob\Desktop\example.txt'''

As this section of the docs states:

The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

Unless the string is prefixed with an r, escape sequences are interpreted as follows:

Escape SequenceMeaning
{newline}Backslash and newline ignored
\Backslash (\)
\'Single quote (')
\"Double quote (")
\nASCII Linefeed
\rASCII Carriage Return
\tASCII Horizontal Tab

A backslash is also used as a continuation character.

main.py
my_str = 'first \ second \ third' print(my_str) # first second third

When a backslash is added at the end of a line, the newline is ignored.

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.