Reason: Window can read files with \, but in the string \ is used as an escape character, after the escape may not find the path of the resource, for example \t will be escaped as tab key
Upper Code:
>>> def func1(path_name): ... import os ... if os.path.exists(path_name): ... return "True" ... else: ... return "False" ... >>> func1("C:\Users\renyc")#error File "<stdin>", line 1 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape >>>
In this example: "C:\Users\renyc" is escaped and the resource of the path is not found.
Solution:
>>> def func1(path_name): ... import os ... if os.path.exists(path_name): ... return "True" ... else: ... return "False" ... >>> func1(r"C:\Users\renyc")#Add r to declare the string without escaping 'True' >>> func1("C:\\Users\\\renyc")# handling of absolute paths 'True' >>>
There are three ways to summarize.
One:Replace the write method with an absolute path func1(“C:\Users\renyc”)
Two: explicitly declare the string without escaping (add r) func1(r “C:\Users\renyc”)
III:Use Linux paths/ func1(“C:/Users/renyc”)
Read More:
- SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UX
- SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UX
- [Solved] SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 6-7: malformed
- [Solved] SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 10-11: malformed
- [Solved] SyntaxError: (unicode error) ‘utf-8’ codec can’t decode byte 0xbe in position 0
- How to Solve Pytorch DataLoader Loading Error: UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe5 in position 1023
- [Solved] UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xbf in position 7: invalid start byte
- [Solved] Grid Search Error (GridSearchCV): ‘ascii‘ codec can‘t encode characters in position 18-20: ordinal not in r
- [Solved] git review Error: UnicodeDecodeError: ‘gbk‘ codec can‘t decode
- [Exception]’ascii’ codec can’t decode byte 0xe8 in position 2: ordinal not in range(128)
- Django CSV file Error: UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb5 in position 0: invalid start
- Typeerror in Python regular expression: expected string or bytes like object
- Pychar can’t connect to Python console, but it can run. Py file, and Anaconda’s command line can run Python command
- [Solved] RuntimeError: function ALSQPlusBackward returned a gradient different than None at position 3, but t
- You can run the Ansible Playbook in Python by hand
- AttributeError: can‘t set attribute [How to Solve]
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- Autograd error in Python: runtimeerror: grad can be implicitly created only for scalar outputs
- Python 3.7 Error: AttributeError: ‘str‘ object has no attribute ‘decode‘ [How to Solve]
- [Solved] Python operate Kafka error: SyntaxError: invalid syntax