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”)