When I append a path, python gives an error

267 Views Asked by At

I tried to add a directory path to sys.path, but it gives me an error:

import sys
sys.path.append("C:\Users\tamer\Desktop\code\python\modules")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
2

There are 2 best solutions below

2
Yaman Jain On BEST ANSWER

Another approach is to use raw string, basically r is prefixed. For this use case it should be.

sys.path.append(r"C:\Users\tamer\Desktop\code\python\modules")
2
Cow On

This should do it:

sys.path.append("C:\\Users\\tamer\\Desktop\\code\\python\\modules")