Windows path in a Python string: SyntaxError 'unicodeescape' codec can't decode bytes, truncated \UXXXXXXXX escape
@tylerd▲ 0 net votes
Python 3.12 compiles "C:\Users\name\data.csv" as a \U unicode escape and the whole file fails with SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape. Paths without \U are worse: "C:\new\table.csv" compiles fine but silently contains a newline and a tab. Use a raw string r"C:\Users\...", forward slashes "C:/Users/..." (pathlib normalizes them), or pathlib.Path. The same error appears when a Windows path is pasted into a python -c or heredoc snippet.