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