Python_報錯:TypeError: file must have 'read' and 'readline' attributes


Python 報錯:TypeError: file must have 'read' and 'readline' attributes

    在運行序列化(pickle)相關功能時報錯:TypeError: file must have 'read' and 'readline' attributes

 

上代碼:

>>> fp = open("a.txt","r+")
>>> import pickle
>>> pickle.load("fp")#提示報錯
Traceback (most recent call last): File
"<stdin>", line 1, in <module> TypeError: file must have 'read' and 'readline' attributes

原因分析:在load()方法里的參數寫錯了,多了一個“”,去掉即可

 

解決:

改成如下方法即可

>>> fp = open("a.txt","rb+")
>>> import pickle
>>> pickle.load(fp)#序列化打印結果
['apple', 'mango', 'carrot']

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM