Home › Python

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

  File "script.py", line 3
    df = pd.read_csv("C:\Users\me\Desktop\data.csv")
                                                  ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Quick fix: Prefix the string with r: r"C:\Users\me\data.csv", or use forward slashes "C:/Users/me/data.csv", or double every backslash "C:\\Users\\me\\data.csv". Better still, build paths with pathlib.Path.

What it means

In a normal Python string, a backslash starts an escape sequence: \n is a newline, \t is a tab, and \U must be followed by 8 hex digits (\U0001F600). In the path "C:\Users\...", Python sees \U followed by sers (not hex) and refuses to compile the file. That's why this is a SyntaxError that appears before any code runs.

Variants of the same problem:

Fix 1: raw string

Prefix the literal with r so backslashes are kept as-is:

df = pd.read_csv(r"C:\Users\me\Desktop\data.csv")

One catch: a raw string can't end with a single backslash (r"C:\folder\" is a syntax error). Leave the trailing backslash off, or use Fix 2 or Fix 4.

Fix 2: forward slashes

Windows accepts / as a path separator in nearly all APIs:

df = pd.read_csv("C:/Users/me/Desktop/data.csv")

Fix 3: escape each backslash

df = pd.read_csv("C:\\Users\\me\\Desktop\\data.csv")

Fix 4: pathlib (best for real code)

from pathlib import Path

data = Path.home() / "Desktop" / "data.csv"
df = pd.read_csv(data)

No hard-coded separators, and it works on every OS. Path(__file__).parent / "data.csv" gives a path relative to the script instead of the current working directory.

Same error in other places

UnicodeDecodeError: 'unicodeescape' at runtime

If you see this while running code (not compiling), something is calling .decode("unicode_escape") or codecs.decode(s, "unicode_escape") on text that contains Windows paths. Don't decode paths that way. Treat them as plain strings.

Last updated 2026-11-15.