Python 为什么我在读取空文件时会收到“Pickle - EOFError: Ran out of input”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24791987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
提问by Magix
I am getting an interesting error while trying to use Unpickler.load()
, here is the source code:
我在尝试使用时遇到一个有趣的错误Unpickler.load()
,这是源代码:
open(target, 'a').close()
scores = {};
with open(target, "rb") as file:
unpickler = pickle.Unpickler(file);
scores = unpickler.load();
if not isinstance(scores, dict):
scores = {};
Here is the traceback:
这是回溯:
Traceback (most recent call last):
File "G:\python\pendu\user_test.py", line 3, in <module>:
save_user_points("Magix", 30);
File "G:\python\pendu\user.py", line 22, in save_user_points:
scores = unpickler.load();
EOFError: Ran out of input
The file I am trying to read is empty. How can I avoid getting this error, and get an empty variable instead?
我试图读取的文件是空的。我怎样才能避免出现这个错误,而是得到一个空变量?
采纳答案by Padraic Cunningham
I would check that the file is not empty first:
我会先检查文件是否为空:
import os
scores = {} # scores is an empty dict already
if os.path.getsize(target) > 0:
with open(target, "rb") as f:
unpickler = pickle.Unpickler(f)
# if file is not empty scores will be equal
# to the value unpickled
scores = unpickler.load()
Also open(target, 'a').close()
is doing nothing in your code and you don't need to use ;
.
也在open(target, 'a').close()
您的代码中什么也不做,您不需要使用;
.
回答by jramirez
You can catch that exception and return whatever you want from there.
您可以捕获该异常并从那里返回您想要的任何内容。
open(target, 'a').close()
scores = {};
try:
with open(target, "rb") as file:
unpickler = pickle.Unpickler(file);
scores = unpickler.load();
if not isinstance(scores, dict):
scores = {};
except EOFError:
return {}
回答by Amr Ayman
As you see, that's actually a natural error ..
如您所见,这实际上是一个自然错误..
A typical construct for reading from an Unpickler object would be like this ..
从 Unpickler 对象读取的典型构造是这样的..
try:
data = unpickler.load()
except EOFError:
data = list() # or whatever you want
EOFError is simply raised, because it was reading an empty file, it just meant End of File..
EOFError 只是引发了,因为它正在读取一个空文件,它只是意味着文件结束..
回答by jukoo
if path.exists(Score_file):
try :
with open(Score_file , "rb") as prev_Scr:
return Unpickler(prev_Scr).load()
except EOFError :
return dict()
回答by Abhay Nainan
Most of the answers here have dealt with how to mange EOFError exceptions, which is really handy if you're unsure about whether the pickled object is empty or not.
这里的大多数答案都涉及如何管理 EOFError 异常,如果您不确定腌制对象是否为空,这真的很方便。
However, if you're surprised that the pickle file is empty, it could be because you opened the filename through 'wb' or some other mode that could have over-written the file.
但是,如果您对 pickle 文件为空感到惊讶,那可能是因为您通过“wb”或其他可能覆盖文件的模式打开了文件名。
for example:
例如:
filename = 'cd.pkl'
with open(filename, 'wb') as f:
classification_dict = pickle.load(f)
This will over-write the pickled file. You might have done this by mistake before using:
这将覆盖腌制文件。您可能在使用之前错误地执行了此操作:
...
open(filename, 'rb') as f:
And then got the EOFError because the previous block of code over-wrote the cd.pkl file.
然后得到 EOFError 因为前面的代码块覆盖了 cd.pkl 文件。
When working in Jupyter, or in the console (Spyder) I usually write a wrapper over the reading/writing code, and call the wrapper subsequently. This avoids common read-write mistakes, and saves a bit of time if you're going to be reading the same file multiple times through your travails
在 Jupyter 或控制台 (Spyder) 中工作时,我通常会在读/写代码上编写一个包装器,然后调用包装器。这避免了常见的读写错误,如果您要多次读取同一个文件,还可以节省一些时间
回答by user2723494
It is very likely that the pickled file is empty.
腌制文件很可能是空的。
It is surprisingly easy to overwrite a pickle file if you're copying and pasting code.
如果您正在复制和粘贴代码,覆盖pickle 文件非常容易。
For example the following writes a pickle file:
例如下面写了一个pickle文件:
pickle.dump(df,open('df.p','wb'))
And if you copied this code to reopen it, but forgot to change 'wb'
to 'rb'
then you would overwrite the file:
如果您复制此代码以重新打开它,但忘记更改'wb'
为,'rb'
那么您将覆盖该文件:
df=pickle.load(open('df.p','rb'))
The correct syntax is
正确的语法是
df=pickle.load(open('df.p','wb'))
回答by ualia Q
Note that the mode of opening files is 'a' or some other have alphabet 'a' will also make error because of the overwritting.
请注意,打开文件的模式是 'a' 或其他一些带有字母 'a' 的文件也会因为覆盖而出错。
pointer = open('makeaafile.txt', 'ab+')
tes = pickle.load(pointer, encoding='utf-8')