Python syntaxerror: (unicode error) 'Unicodeescape' codec can't Decode Bytes in position 2-3: TR

created at 06-26-2021 views: 1

Hello ~ Happy New Year, I haven't updated for a long time, I just got the error SyntaxError: (Unicode Error) 'Unicodeescape' codec can't Decode, which is acts, in position 2-3: Tr . The reason for the mistake is the problem of escape.

For example, the file path I passed in the file is like this.

sys.path.append('c:\Users\mshacxiang\VScode_project\web_ddt')

Cause Analysis: Read the file path in the Windows system to use \, but in the python string, there is a synonymous meaning, such as \ t can represent Tab, \ n represents the wrap, so we need to take some way to make \ not Interpretation is essential characters. There are currently three solutions

1  plus R in front of the path, that is, keep the character's original value.

sys.path.append(r'c:\Users\mshacxiang\VScode_project\web_ddt')

2 replace the double reverse slope

sys.path.append('c:\\Users\\mshacxiang\\VScode_project\\web_ddt')

3  replace it as a forward slash

sys.path.append('c:/Users/mshacxiang/VScode_project/web_ddt')
Please log in to leave a comment.