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