【调试经验】Python - ‘unicodeescape‘ codec can‘t decode bytes in position : truncated \uxxxx escape

报错记录

(unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX escape

报错原因

在字符串中存在字符 '\',而实际意义上的字符串中的 '\' 字符在Python中会被作为转义字符来使用,这就使得原字符串中的字符脱离原意。这个问题一般发生在具有文件路径的字符串中。

解决方法

1. 在字符串前加上r或R

例如:

run (r'E:\Scripts\autorun.bat') 

2. 将转义符'\'转为实际字符

例如:

run ('E:\\Scripts\\autorun.bat') 

3. 将字符 '\' 替换为 '/'

例如:

run ('E:/Scripts/autorun.bat') 

猜你喜欢

转载自blog.csdn.net/weixin_42839065/article/details/131425921
今日推荐