2016-11-10 84 views
-2

晚报所有,EOF错误在Python

我已经做在Python程序,它或多或少存在,但最后的结果是引起EOF错误,我很困惑,为什么或如何解决它!

myFile =open("positionfile.dat", "rb") #opens and reads the file to allow data to be added 
positionlist = pickle.load(myFile) #takes the data from the file and saves it to positionlist 
individualwordslist = pickle.load(myFile) #takes the data from the file and saves it to individualwordslist 
myFile.close() #closes the file 

带着一堆代码在它之前。

的错误是:

Traceback (most recent call last): 
File "P:/A453 scenario 1 + task 3.py", line 63, in <module> 
    individualwordslist = pickle.load(myFile) #takes the data from the file and saves it to individualwordslist 
EOFError 

任何帮助,将不胜感激!

+1

你确定这是你的脚本吗?我看到知道错误。在你当前的程序中。 –

+1

我们需要了解数据如何写入文件。 –

+1

你在抱怨解析文件时出现错误。您既不显示该文件中的内容,也不显示它是如何创建的。有人会怎么回答?如果你的课程需要帮助,请与你的老师谈谈,以确保你没有作弊。 – jonrsharpe

回答

1

你在同一个文件上打两次pickle.load()。第一次调用将读取整个文件,将文件指针留在文件末尾,因此为EOFError。您需要在第二次调用之前使用file.seek(0)重置文件开头处的文件指针。

>> import pickle 
>>> wot = range(5) 
>>> with open("pick.bin", "w") as f: 
...  pickle.dump(wot, f) 
... 
>>> f = open("pick.bin", "rb") 
>>> pickle.load(f) 
[0, 1, 2, 3, 4] 
>>> pickle.load(f) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.7/pickle.py", line 1378, in load 
    return Unpickler(file).load() 
    File "/usr/lib/python2.7/pickle.py", line 858, in load 
    dispatch[key](self) 
    File "/usr/lib/python2.7/pickle.py", line 880, in load_eof 
    raise EOFError 
EOFError 
>>> f.seek(0) 
>>> pickle.load(f) 
[0, 1, 2, 3, 4] 
>>>