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']
Similar Posts:
- [Solved] Python error: EOFError: Ran out of input
- Python TypeError: unbound method a() must be called with A instance as first argument (go…
- How to use htmltestrunner.py to report importerror: no module named ‘stringio’ in Python 3
- [Solved] Python Numpy Data load error: Unicode error: unpicking a python object failed: Unicode decodeerror
- Error reporting using Yum: Yum except keyboardinterrupt, e
- [Solved] PythonTypeError: ‘<' not supported between instances of 'str' and 'int'
- [Solved] Importerror: DLL load failed while importing mtrand: the specified program could not be found.
- [Solved] PostgreSQL configure: error: readline library not found
- Dill: solve the problem of Python’s “attributeerror: can’t pickle local object” and unable to pickle lambda function
- Python: How to Modify AD Password