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

MRAB python at mrabarnett.plus.com
Fri May 8 15:33:25 EDT 2015


On 2015-05-08 20:00, zljubisicmob at gmail.com wrote:
> The script is very simple (abc.txt exists in ROOTDIR directory):
>
> import os
> import shutil
>
> ROOTDIR = 'C:\Users\zoran'
>
> file1 = os.path.join(ROOTDIR, 'abc.txt')
> file2 = os.path.join(ROOTDIR, 'def.txt')
>
> shutil.move(file1, file2)
>
>
> But it returns the following error:
>
>
> C:\Python34\python.exe C:/Users/bckslash_test.py
>    File "C:/Users/bckslash_test.py", line 4
>      ROOTDIR = 'C:\Users'
>               ^
> SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>
> Process finished with exit code 1
>
> As I saw, I could solve the problem by changing line 4 to (small letter "r" before string:
> ROOTDIR = r'C:\Users\zoran'
>
> but that is not an option for me because I am using configparser in order to read the ROOTDIR from underlying cfg file.
>
> I need a mechanism to read the path string with single backslashes into a variable, but afterwards to escape every backslash in it.
>
> How to do that?
>
If you're reading the path from a file, it's not a problem. Try it!




More information about the Python-list mailing list