(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escap

代码如下:

import  os
path = 'C:\Users\cheng\Downloads'
print ("os.path.getsize(path):",os.path.getsize(path))       #返回文件大小,如果文件不存在就返回错误
print ("os.path.isabs(path):",os.path.isabs(path))

(unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

原因:
在Python中\是转义符,\u表示其后是UNICODE编码,因此\User在这里会报错,在字符串前面加个r表示就可以了
修改之后如下所示:


path = r'C:\Users\cheng\Downloads'

你可能感兴趣的:(python)