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

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

时间: 2024-08-26 01:00:32

Python_报错:TypeError: file must have 'read' and 'readline' attributes的相关文章

Python_报错:SyntaxError: (unicode error) &#39;unicodeescape&#39; codec can&#39;t decode bytes in position 2-3: truncated \UXXXXXXXX escape

Python运行后,报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 原因:window 读取文件可以用\,但是在字符串中\是被当作转义字符来使用,经过转义之后可能就找不到路径的资源了,例如\t会转义为tab键 上代码: >>> def func1(path_name): ... import os ..

Python_报错:SyntaxError: EOL while scanning string literal

Python_报错:SyntaxError: EOL while scanning string literal 原因:python中,目录操作时,字符串的最后一个字符是斜杠,会导致出错,去掉\即可 上代码 >>> import os >>> os.chdir(r"e:\")#字符串的最后一个字符是斜杠,会导致出错 File "<stdin>", line 1 os.chdir(r"e:\") ^

【转】 grep 文件报错 “Binary file ... matches”

原文链接 http://blog.csdn.net/yaochunnian/article/details/7261006 grep 文件报错 “Binary file ... matches” 原因:文件为binary文件 解决:strings vers.log.2010-03-09 | grep -i ‘mezimedia’ 或者 grep -a -i ‘mezimedia’ vers.log.2010-03-09 grep命令是linux下的行过滤工具,其参数繁多,下面就一一介绍个个参数的

python框架Scrapy报错TypeError: &#39;float&#39; object is not iterable解决

原因是:Twisted版本高了. 解决办法: 只要把Twisted库降级到16.6.0即可: 1 pip3 install Twisted==16.6.0 2 3 注:Twisted16.6.0安装后,会自动卸载高版本的Twisted python框架Scrapy报错TypeError: 'float' object is not iterable解决

ORACLE 11G收缩表空间报错 ORA-03297: file contains used data beyondrequested RESIZE value

测试环境磁盘空间不足,所以drop一些无用的大表,但是发现空间没有变化,df -h还是没有释放出磁盘空间来. SQL> set line 200 SQL> set pagesize 200 SQL> col name format A150 1,查看表空间使用情况 SQL> SELECTUPPER(F.TABLESPACE_NAME) "表空间名", 2 D.TOT_GROOTTE_MB "表空间大小(M)", 3 D.TOT_GROOTTE

qt5.5.1 苹果电脑10.12.2 编译报错&#39;cstddef&#39; file not found

问题: qt5.5.1   苹果电脑10.12.2  编译报错: /Users/apple/Qt5.5.1/5.5/clang_64/lib/QtCore.framework/Headers/qglobal.h:39: error: 'cstddef' file not found 解决办法: 打开Pro文件, 添加一条 QMAKE_MAC_SDK=macosx10.12 意思就是设置编译SDK 和苹果系统一样. qt5.5.1 苹果电脑10.12.2 编译报错'cstddef' file no

[转载]UEditor报错TypeError: me.body is undefined

本文转载来自:UEditor报错TypeError: me.body is undefined 今天在使用UEditor的setContent的时候报错,报错代码如下 TypeError: me.body is undefined 或 Uncaught TypeError: Cannot set property 'innerHTML' of undefined 错误的原因是没有等UEditor创建完成就使用UEditor的setContent函数了,可以通过如下代码解决 方法一: uedito

python 报错TypeError: &#39;range&#39; object does not support item assignment,解决方法

贴问题 nums = range(5)#range is a built-in function that creates a list of integers print(nums)#prints "[0,1,2,3,4]" print(nums[2:4])#Get a slice from index 2 to 4 (exclusive); prints '[2,3]" print(nums[2:])#Get a slice from index 2 to the end

VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property &#39;xxxx&#39; of undefined 的解决办法

正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of undefined 主要原因是: 在 then的内部不能使用Vue的实例化的this, 因为在内部 this 没有被绑定.可以看下 Stackoverflow 的解释: 解决办法: 1.用ES6箭头函数,箭头方法可以和父方法共享变量 2.在请求axios外面定义一下 var that=this 问题