Tag Archives: Python problem analysis and solution

SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UX

Cause analysis: Python \ has the meaning of escape, so the read path is wrong

import os
os.walk('C:\Users\user\Desktop\Test')

Solution:
1. Add r in front of the path to prevent character escape.

import os
os.walk(r'C:\Users\user\Desktop\Test')

2. Use a double diagonal bar to read and output a backslash

import os
os.walk(r'C:\\Users\\user\\Desktop\\Test')