Last updated: Apr 8, 2024
Reading time·3 min
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'
.
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.
# ⛔️ 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
).
r
to mark it as a raw stringOne way to solve the error is to prefix the string with the letter r
to mark
it as a
raw string.
# ✅ 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)
r
are called raw strings and treat backslashes as literal characters.If the error persists, try to use a triple-quoted raw string instead.
# ✅ 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.
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.
An alternative way to treat a backslash \
as a literal character is to escape
it with a second backslash \\
.
# ✅ 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.
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.
my_str = 'bobby\\\\hadz\\\\com' print(my_str) # 👉️ "bobby\\hadz\\com"
An alternative solution to the error is to use forward slashes in the path instead of backslashes.
# ✅ 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.
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:
r
to mark it as a raw stringHere are the 3 possible solutions to the error.
# ✅ 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.
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 Sequence | Meaning |
---|---|
{newline} | Backslash and newline ignored |
\ | Backslash (\ ) |
\' | Single quote (' ) |
\" | Double quote (" ) |
\n | ASCII Linefeed |
\r | ASCII Carriage Return |
\t | ASCII Horizontal Tab |
A backslash is also used as a continuation character.
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.