Python_报错:TypeError: file must have 'read' and 'readline' attributes

Posted 翻滚的小强

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]

 


以上是关于Python_报错:TypeError: file must have 'read' and 'readline' attributes的主要内容,如果未能解决你的问题,请参考以下文章

Python报错:TypeError: __init__() got an unexpected keyword argument 'io_loop' 原因及解决方案

Python_报错:TypeError: Tuple or struct_time argument required

转python 调用super()初始化报错“TypeError: super() takes at least 1 argument”

python引入模块报错:TypeError: ‘module‘ object is not callable,解决办法记录

python引入模块报错:TypeError: ‘module‘ object is not callable,解决办法记录

Django :执行 python manage.py makemigrations 时报错 TypeError: __init__() missing 1 required positional a