file open error: [Errno 2] No such file or directory: '\xe6\xb5\x8b\xe8\xaf\x95.txt'
当我们用python访问一个中文命名的文本时,经常会遇到如下错误:
file open error: [Errno 2] No such file or directory: '\xe6\xb5\x8b\xe8\xaf\x95.txt'
例如下面的代码:
# coding: UTF-8
import os
try:fin=open('测试.txt','r')
except IOError,e:print "file open error:",e
else:for eachline in fin:print eachline,
fin.close()
这是由于python默认的编码格式为unicode。
因此我们需要将GBK格式的‘测试’解码为“UTF-8”,如下:
# coding: UTF-8
import os
filename='测试.txt'
try:fin=open(filename.decode('UTF-8'),'r')
except IOError,e:print "file open error:",e
else:for eachline in fin:print eachline,
fin.close()
这样就可以正常访问文件了:
file open error: [Errno 2] No such file or directory: '\xe6\xb5\x8b\xe8\xaf\x95.txt'相关推荐
- python multiprocessing manager list error: [Errno 2] No such file or directory
python multiprocessing manager list error: [Errno 2] No such file or directory for i in range(AMOUNT ...
- 执行 redis-dump 报错:ERROR (Errno::ENOENT): No such file or directory - ps -o rss= -p xxxxx
redis-dump 命令用于 Redis 数据的导出,执行该命令时报错如下: C:\Users\Lenovo>redis-dump -u 127.0.0.1:6379 ERROR (Errno ...
- C:\Python27\python.exe: can't open file '2.py': [Errno 2] No such file or directory
最近开始学习了"笨方法学python",在练习的第一个程序就出现了如下错误: C:\Python27\python.exe: can't open file '2.py': [Er ...
- python Django创建数据库时can't open file 'manage.py': [Errno 2] No such file or directory
1.在学习Diango新建项目后,进行创建数据库的时候,使用以下命令: python manage.py migrate 2.发现竟然报can't open file 'manage.py': [Er ...
- keras跑yolov3模型报错1“python3: can't open file 'convert.py': [Errno 2] No such file or directory”解决
刚开始学yolo和keras,跑github上模型 https://github.com/qqwweee/keras-yolo3 一开始遇到问题,让运行"python convert.py ...
- 服务器 python cant open file_python: can't open file 'h.py': [Errno 2] No such file or directory
这是在写第一个程序时就遇到的问题: python: can't open file 'h.py': [Errno 2] No such file or directory 我建了一个文件夹,命名为&q ...
- 使用yarn install时报错:00h00m00s 0/0: : ERROR: [Errno 2] No such file or directory: ‘install‘
执行以下命令: sudo apt remove cmdtest sudo apt remove yarn sudo npm install -g yarn 注意: 指令命令后需要重启终端再输入yarn ...
- 创建数据库python: can‘t open file ‘manage.py‘: [Errno 2] No such file or directory报错
https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注! 欢迎关注微信公众号:宝藏女孩的成长日记 如有转载,请注明出处(如不注明,盗者必究) 在创建数据库时运行 ...
- jenkins 构建提示 can‘t open file xxxx [Errno 2] No such file or directory
在windows环境中使用jenkins构建python脚本运行失败 检查控制台提示:python: can't open file 'run_all_case.py': [Errno 2] No s ...
- ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: ‘
ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: ' ...
最新文章
- 病毒研究之感染linux脚本
- Web socket广播
- 【CodeForces - 697D】Puzzles(树形dp,期望dp)
- phpcms网站搬家至服务器
- 动态规划——小A点菜(洛谷 P1164)
- springboot框架学习理解下
- ~0 == -1 问题全解
- 大连雅思培训百家国际雅思考试词汇量要求与记忆方法是什么
- linux开启vt虚拟化,VT虚拟化如何开启
- 遇到电脑任务栏卡死怎么办
- Silverlight 中文教程第五部分:用 ListBox 和 DataBinding 显示列表数据 (木野狐译)
- pg_receivewal实践
- windows debug tools
- 【Java容器】Java容器入门教程
- 客户案例 | 高等教育出版社
- Wikipedia iOS客户端源码
- java 邮件 已读回执_java – 在Firebase群组消息传递应用中实施已读回执功能
- 会员价值分析-波士顿矩阵
- antV实现离线中国2D地图并叠加拓扑(一)
- 工业智能网关解决方案:物联网仓储环境监测系统