182

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?

Magix
  • 4,359
  • 6
  • 22
  • 46

11 Answers11

260

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:

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.

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

Abhay Nainan
  • 3,036
  • 1
  • 11
  • 13
172

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 ;.

0 _
  • 9,294
  • 10
  • 71
  • 105
Padraic Cunningham
  • 168,988
  • 22
  • 228
  • 312
  • open(target, 'a').close() is here to make sure the file exists ;-) + I don't need to use `;` but I just came from C, and not using `;` at the end of my lines make my cry T.T – Magix Jul 16 '14 at 23:00
  • ok, but the issinstance is unnecessary as I imagine you are only going to be pickling a dict, checking for an empty file will suffice – Padraic Cunningham Jul 16 '14 at 23:01
  • furthermore, checking the file is not empty will not always mean I can unpickle it... raising an exception... That's why I don't think your answer is not the best, even thought it is not bad. – Magix Jul 16 '14 at 23:04
  • 2
    catching an `EOF exception` won't save you from all the other potential errors. – Padraic Cunningham Jul 16 '14 at 23:06
  • You are right, that's why I will also check for other errors... catching EOFError only is good here, but catching all other error types are better, and this is more clever to do it this way, as I am sure the program will not be stopped by a dictionnary-reading function which is not that important in the program. – Magix Jul 16 '14 at 23:09
  • 2
    you can check if a file exists using the os module also, might be better than opening and closing a file every time . – Padraic Cunningham Jul 16 '14 at 23:12
23

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:

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:

df=pickle.load(open('df.p','wb'))

The correct syntax is

df=pickle.load(open('df.p','rb'))
user2723494
  • 990
  • 1
  • 13
  • 22
  • 1
    The last two code examples should be swapped, right? – Daniello Aug 17 '20 at 20:12
  • Yes, I made the same mistake and all the results are ruined which had to rerun all the previous calculations and wait for one day to get the results. What a pity! – sikisis Sep 07 '20 at 23:15
9

As you see, that's actually a natural error ..

A typical construct for reading from an Unpickler object would be like this ..

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 ..

0 _
  • 9,294
  • 10
  • 71
  • 105
Amr Ayman
  • 1,079
  • 1
  • 8
  • 23
3

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 {}
jramirez
  • 8,201
  • 7
  • 31
  • 46
2
if path.exists(Score_file):
      try : 
         with open(Score_file , "rb") as prev_Scr:

            return Unpickler(prev_Scr).load()

    except EOFError : 

        return dict() 
jukoo
  • 21
  • 2
1

I have encountered this error many times and it always occurs because after writing into the file, I didn't close it. If we don't close the file the content stays in the buffer and the file stays empty. To save the content into the file, either file should be closed or file_object should go out of scope.

That's why at the time of loading it's giving the ran out of input error because the file is empty. So you have two options :

  1. file_object.close()
  2. file_object.flush(): if you don't wanna close your file in between the program, you can use the flush() function as it will forcefully move the content from the buffer to the file.
Lakshika Parihar
  • 729
  • 6
  • 16
  • 1
    also in my case I see from a jupyter notebook execution that, if size of object serialised is too big for memory it raises the same error. – serpiko Aug 11 '21 at 14:57
0

Note that the mode of opening files is 'a' or some other have alphabet 'a' will also make error because of the overwritting.

pointer = open('makeaafile.txt', 'ab+')
tes = pickle.load(pointer, encoding='utf-8')
ualia Q
  • 21
  • 3
0
temp_model = os.path.join(models_dir, train_type + '_' + part + '_' + str(pc))
# print(type(temp_model)) # <class 'str'>
filehandler = open(temp_model, "rb")
# print(type(filehandler)) # <class '_io.BufferedReader'>
try:
    pdm_temp = pickle.load(filehandler)
except UnicodeDecodeError:
    pdm_temp = pickle.load(filehandler, fix_imports=True, encoding="latin1")
0

Had the same issue. It turns out when I was writing to my pickle file I had not used the file.close(). Inserted that line in and the error was no more.

lee
  • 1
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31201196) – Sunderam Dubey Mar 07 '22 at 17:09
0
from os.path import getsize as size
from pickle import *
if size(target)>0:
    with open(target,'rb') as f:
        scores={i:j for i,j in enumerate(load(f))}
else: scores={}
Myk
  • 1
  • 1
  • 4
    Could you provide a short explanation as to how your code works? – BrokenBenchmark May 26 '22 at 20:53
  • What is exactly not clear for You? Which line in code is? We have a limited size of answer-area, i cannt write hier to much. – Myk Jun 01 '22 at 16:12
  • 1
    That's not true: you can use as much space as you need to write your answer. It's worth making it clear, if you want others to benefit from it in the future. – joanis Jun 01 '22 at 21:39