码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-10-22 22:23:50      阅读:457      评论:0      收藏:0      [点我收藏+]

标签:app   load   code   ack   ror   port   apple   readline   recent   

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

标签:app   load   code   ack   ror   port   apple   readline   recent   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!