Why am I getting "Pickle - EOFError: Ran from Input" reading an empty file?

I am getting an interesting error when trying to use Unpickler.load()

, here is the original 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's the trace:

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?

+72


source to share


6 answers


I would check that the file is not empty at 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()

does nothing in your code and you don't need to use ;

.

+85


source


Most of the answers here have dealt with how to handle EOFError exceptions, which is very handy if you're not sure if the salted object is empty or not.

However, if you are surprised that the pickle file is empty, it could be because you opened the file name with 'wb' or some other mode that might overwrite the file.

eg:

filename = 'cd.pkl'
with open(filename, 'wb') as f:
    classification_dict = pickle.load(f)

      



This will overwrite the pickled file. You may have done this by mistake before using:

...
open(filename, 'rb') as f:

      

And then got an EOFError because the previous block of code rewrote the cd.pkl file.

When working in Jupyter or Console (Spyder), I usually write a wrapper on top of the read / write code and call the wrapper afterwards. This avoids common read and write errors and saves a little time if you read the same file multiple times in difficulty.

+75


source


As you can see, this is actually a natural mistake.

A typical construction for reading from an Unpickler object would look like this.

try:
    data = unpickler.load()
except EOFError:
    data = list()  # or whatever you want

      

EOFError is just raised because it reads an empty file, it just means End of file ..

+3


source


You can catch this exception and return whatever you want.

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 {}

      

+1


source


if path.exists(Score_file):
      try : 
         with open(Score_file , "rb") as prev_Scr:

            return Unpickler(prev_Scr).load()

    except EOFError : 

        return dict() 

      

+1


source


It is very likely that the pickled file is empty.

It's surprisingly easy to overwrite a pickle file if you copy and paste the code.

For example, the following file writes a brine 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 overwrite the file:

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

      

The correct syntax is:

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

      

0


source







All Articles