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
- Python traverses all files under the specified path and retrieves them according to the time interval
- pd.to_csv Error: need to escape, but no escapechar set
- [Solved] SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 6-7: malformed
- pytorch RuntimeError: Error(s) in loading state_ Dict for dataparall… Import model error solution
- Python 3 uses the relative path import module
- Python FileNotFoundError: [Errno 2] No such file or directory: ‘objects/epsilon.pkl
- Tensorflow: Common Usage of tf.get_variable_scope()
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- The range of Chinese Unicode encoding and the calculation of the number of Chinese and English words in text
- Python raspberry pie starts sending IP address to mailbox
- Jsondecodeerror error: json.decoder.JSONDecodeError: Expecting value: line 1 column 26 (char 25)
- Python: How to parses HTML, extracts data, and generates word documents
- Python parsing JSON Error: NameError: name ‘false’ is not defined
- [Solved] Python Project Import Module Error: ModuleNotFoundError
- Python installs virtualenv through PIP and always reports an error: response.py“, line 438, in _error_catcher yield
- [Solved] Python opencv imshow Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow
- [Solved] ParserError: NULL byte detected. This byte cannot be processed in Python‘s native csv library
- Pandas read_csv pandas.errors.ParserError: Error tokenizing data