Run the python file and report SyntaxError: (unicode error)'unicodeescape' codec can't decode bytes in position 2-3: tr

created at 10-08-2021 views: 1

When I was running the python file, it reported

SyntaxError: (unicode error)'unicodeescape' codec can't decode bytes in position 2-3: tr

Error, in fact, the cause of this error is the problem of escaping.

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

Cause Analysis

You can use \ to read the file path in the windows system, but \ has the meaning of escape in the python string,

For example, \t can represent TAB, and \n represents newline, so we need to take some measures to prevent \ from being interpreted as escape characters. There are currently 3 solutions

1. Add r in front of the path to keep the original value of the character.

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

2. Replace with double backslashes

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

3. Replace with forward slash

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