python的pickle在load的时候报 Ran out of input是什么原因?

神圣之风
  • 470
import pickle
a=[1,2,3,4,5]
f=open("d:/pickletest.pk","wb")
pickle.dump(a,f)
print("-=-=-=-=\n")

fr=open("d:/pickletest.pk","rb")
rpk=pickle.load(fr)
print(type(rpk),"[]",rpk)
print("end")

clipboard.png

Traceback (most recent call last):
  File "test1.py", line 8, in <module>
    rpk=pickle.load(fr)
EOFError: Ran out of input
回复
阅读 7.4k
3 个回答
✓ 已被采纳

在操作文件的时候推送这样的写法,这样会自动释放

with open("d:/pickletest.pk","wb") as f:
    pickle.dump(a,f)
  

楼上正解,善用with open()

自问自答,加一句f.close()就好了。。

宣传栏