Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解决方案
2018-03-09 10:11
591 查看
Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解决方案
转载2018年03月9日 09:06:52标签:SyntaxErro
Python 中 ‘unicodeescape’ codec can’t decode bytes in position XXX: trun 错误解决方案
背景描述
今天在运用Python pillow模块处理图片时遇到一个错误
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape1
刚开始以为是图片名字有中文,不识别,于是在python文件的头部加上
#-*- coding:utf-8 -*-1
但是加完这个还是报错,然后我就把图片的中文去掉还成英文,然后报错,一脸懵逼呀。后来在stackoverflow 上找到了类似的错误,原来是图片路径写的 有问题,错误代码如下
im = Image.open('C:\Users\FrankYuan\Pictures\Camera Roll\WIN_20161010_08_51_57_Pro.jpg')1
正确结果
im = Image.open('C:\\Users\\FrankYuan\\Pictures\\Camera Roll\\WIN_20161010_08_51_57_Pro.jpg')1
或者
im = Image.open(r'C:\Users\FrankYuan\Pictures\Camera Roll\WIN_20161010_08_51_57_Pro.jpg')1
抑或
im = Image.open(r'C:/Users/FrankYuan/Pictures/Camera Roll/WIN_20161010_08_51_57_Pro.jpg')1
原因:
window 读取文件可以用\,但是在字符串中\是被当作转义字符来使用,所以’d:\a.txt’会被转义成’d:\a.txt’这是正确路径,所以不会报错。而‘C:\Users\FrankYuan\Pictures\Camera Roll\WIN_20161010_08_51_57_Pro.jpg ’中经过转义之后可能就找不到路径的资源了,例如\t可能就转义成tab键了。解决办法
python在描述路径时可以有多种方式,现列举常见的三种方式一:转义的方式 'd:\\a.txt' 方式二:显式声明字符串不用转义 'd:r\a.txt' 方式三:使用Linux的路径/ 'd:/a.txt'建议使用正则,使用起来比较简单
dir = r"C:\Users\Administrator\AppData\Roaming\xunjieliuchengtu"
判断path是否是文件
#os.path.isfile(path)
dir = r"C:\Users\Administrator\AppData\Roaming"
if os.path.isfile(dir):
print("file exists")
else:
print("no exists")
相关文章推荐
- Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解决方案
- Python 中 ‘unicodeescape’ codec can’t decode bytes in position XXX: trun错误原因分析及解决方案
- Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解
- 解决Python中导出图片出现的错误SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position
- Python--错误SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: tru
- Python中UnicodeDecodeError:‘XXX’ codec can’t decode bytes in position错误信息解决办法
- Python报错:'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
- Python 报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3:
- python 中(unicode error)'unicodeescape' codec can't decode bytes in position 102-103:truncated\UXXXXX
- Python脚本报错 (unicode error) 'unicodeescape' codec can't decode bytes in position 33-34: truncated
- python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: trunca
- UnicodeDecodeError: 'rawunicodeescape' codec can't decode bytes in position 80-81: truncated \UXXX
- 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
- SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3:
- “Unicode Error ”unicodeescape" codec can't decode bytes… Cannot open text files in Python 3
- SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3解决方法
- python编码错误:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal not in
- (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escap
- SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UX