首页 > 编程语言 > 详细

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

时间:2018-10-22 22:02:58      阅读:996      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/rychh/p/9833247.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!