python读取文件遇到问题及解决


  用python的open()函数打开文件时,

1、文件写绝对路径报IOError: [Errno 2] No such file or directory。文件改为相对路径(只写文件名)解决该问题

2、文件是docx类型,如下代码执行时报TypeError: file() takes at most 3 arguments (4 given)path = r'file2.docx#ignore 忽略错误

path = r'file2.docx'
#ignore 忽略错误
f = open(path, 'r',encoding = 'utf-8',errors = 'ignore'

引入io模块,解决该问题,代码如下:
import io
path = r'file2.docx'
#ignore 忽略错误
f = io.open(path, 'r',encoding = 'utf-8',errors = 'ignore')


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM