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')

posted on 2020-05-27 15:37  希望的天空  阅读(882)  评论(0编辑  收藏  举报