Python Error: TypeError: file must have ‘read’ and ‘readline’ attributes
Error when running serialization (pickle) related functions: TypeError: file must have ‘read’ and ‘readline’ attributes
Code above.
>>> fp = open("a.txt","r+")
>>> import pickle
>>> pickle.load("fp")#error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: file must have 'read' and 'readline' attributes
Cause analysis: the parameter in the load () method is wrongly written. If there is one more “” and it can be removed
Solution:
Change to the following method
>>> fp = open("a.txt","rb+")
>>> import pickle
>>> pickle.load(fp)#Serialized Print Results
['apple', 'mango', 'carrot']