python异常之EOFError: Ran out of input
- 出现该问题一般是由于你在以写模式打开文件后未关闭的情况下又去以读模式操作该文件时报的错误
# coding = utf-8 import pickle #定义一个boy类 class boy(): def __init__(self,name,age): self.name = name self.age = age self.gf_dic = {} #创建实例对象 b = boy('李明',23) f = open("a.text",'wb') pickle.dump(b,f) b1 = pickle.load(open("a.text",'rb')) print(b1.gf_dic) b1.gf_dic[1] = {b:'秀秀'} b1.gf_dic[2] = {b:'丽丽'} pickle.dump(b1,f)
解决:(分段执行)
1 # coding = utf-8 2 import pickle 3 #定义一个boy类 4 class boy(): 5 def __init__(self,name,age): 6 self.name = name 7 self.age = age 8 self.gf_dic = {} 9 #创建实例对象 10 b = boy('李明',23) 11 # f = open("a.text",'wb') 12 # pickle.dump(b,f) 13 # f.close() 14 print(b.gf_dic) 15 16 f1 = open("a.text",'rb') 17 b1 = pickle.load(f1) 18 f1.close() 19 print(b1.gf_dic) 20 # b1.gf_dic[1] = {b:'秀秀'} 21 # b1.gf_dic[2] = {b:'丽丽'} 22 # f2 = open("a.text",'wb') 23 # pickle.dump(b1,f2) 24 # f2.close()
结果:
{}
{1: {<__main__.boy object at 0x000000000277C7B8>: '秀秀'}, 2: {<__main__.boy object at 0x000000000277C7B8>: '丽丽'}}