I am getting an interesting error while trying to use Unpickler.load()
, here is the source code:
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?
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.
For example the following writes a pickle file:
And if you copied this code to reopen it, but forgot to change
'wb'
to'rb'
then you would overwrite the file:The correct syntax is
Note that the mode of opening files is 'a' or some other have alphabet 'a' will also make error because of the overwritting.
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.
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.
for example:
This will over-write the pickled file. You might have done this by mistake before using:
And then got the EOFError because the previous block of code over-wrote the cd.pkl file.
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
You can catch that exception and return whatever you want from there.
As you see, that's actually a natural error ..
A typical construct for reading from an Unpickler object would be like this ..
EOFError is simply raised, because it was reading an empty file, it just meant End of File ..
I would check that the file is not empty first:
Also
open(target, 'a').close()
is doing nothing in your code and you don't need to use;
.