当我们用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'相关推荐

  1. 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 ...

  2. 执行 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 服务器 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 ...

  7. 使用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 ...

  8. 创建数据库python: can‘t open file ‘manage.py‘: [Errno 2] No such file or directory报错

    https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注! 欢迎关注微信公众号:宝藏女孩的成长日记 如有转载,请注明出处(如不注明,盗者必究) 在创建数据库时运行 ...

  9. 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 ...

  10. 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: ' ...

最新文章

  1. 病毒研究之感染linux脚本
  2. Web socket广播
  3. 【CodeForces - 697D】Puzzles(树形dp,期望dp)
  4. phpcms网站搬家至服务器
  5. 动态规划——小A点菜(洛谷 P1164)
  6. springboot框架学习理解下
  7. ~0 == -1 问题全解
  8. 大连雅思培训百家国际雅思考试词汇量要求与记忆方法是什么
  9. linux开启vt虚拟化,VT虚拟化如何开启
  10. 遇到电脑任务栏卡死怎么办
  11. Silverlight 中文教程第五部分:用 ListBox 和 DataBinding 显示列表数据 (木野狐译)
  12. pg_receivewal实践
  13. windows debug tools
  14. 【Java容器】Java容器入门教程
  15. 客户案例 | 高等教育出版社
  16. Wikipedia iOS客户端源码
  17. java 邮件 已读回执_java – 在Firebase群组消息传递应用中实施已读回执功能
  18. 会员价值分析-波士顿矩阵
  19. antV实现离线中国2D地图并叠加拓扑(一)
  20. 工业智能网关解决方案:物联网仓储环境监测系统

热门文章

  1. 路由器下一跳地址怎么判断_路由器工作原理(一)
  2. CSS Sprite(CSS雪碧图)
  3. ARCGIS坐标和CAD坐标显示,以及坐标之间的转换
  4. VMware 虚拟机的使用
  5. vue axio的使用
  6. 手机短信小额支付接口.Net实现
  7. Java个人资产管理系统问题总结
  8. oracle视图、函数、循环、case when
  9. 有关GUASS高斯数据库的语法汇总(获取字符串字节数等)
  10. 年轻时代,噢,年轻时代