EOFError in Python script
Asked Answered
L

4

5

I have the following code fragment:

def database(self):
    databasename=""
    host=""
    user=""
    password=""
    try:
        self.fp=file("detailing.dat","rb")
    except IOError:
        self.fp=file("detailing.dat","wb")
        pickle.dump([databasename,host,user,password],self.fp,-1)
        self.fp.close()
        selffp=file("detailing.dat","rb")
        [databasename,host,user,password]=pickle.load(self.fp)

    return

It has the error:

Traceback (most recent call last):
  File "detailing.py", line 91, in ?
    app=myApp()
  File "detailing.py", line 20, in __init__
    wx.App.__init__(self,redirect,filename,useBestVisual,clearSigInt)
  File "/usr/lib64/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_core.py", line 7473, in __init__
    self._BootstrapApp()
  File "/usr/lib64/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_core.py", line 7125, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "detailing.py", line 33, in OnInit
    self.database()
  File "detailing.py", line 87, in database
    [databasename,host,user,password]=pickle.load(self.fp)
  File "/usr/lib64/python2.4/pickle.py", line 1390, in load
    return Unpickler(file).load()
  File "/usr/lib64/python2.4/pickle.py", line 872, in load
    dispatch[key](self)
  File "/usr/lib64/python2.4/pickle.py", line 894, in load_eof
    raise EOFError
EOFError

What am I doing wrong?

Lorislorita answered 31/3, 2009 at 13:12 Comment(0)
M
14

Unless you've got a typo, the issue may be in this line where you assign the file handle to selffp not self.fp:

selffp=file("detailing.dat","rb")

If that is a typo, and your code actually opens the file to self.fp, then you may wish to verify that the file actually has contents (ie: that the previous pickle worked)... the error suggests that the file is empty.

Edit: In the comments to this answer, S. Lott has a nice summary of why the typo generated the error you saw that I'm pasting here for completeness of the answer: "selffp will be the unused opened file, and self.fp (the old closed file) will be used for the load".

Mincey answered 31/3, 2009 at 13:21 Comment(1)
+1: Excellent spot. selffp will be the unused opened file, and self.fp (the old closed file) will be used for the load.Lur
D
5

Here's the version that I would recommend using:

def database(self):
    databasename=""
    host=""
    user=""
    password=""
    try:
        self.fp=open("detailing.dat","rb")
    except IOError:
        with open("detailing.dat", "wb") as fp:
            pickle.dump([databasename,host,user,password],fp,-1)

        self.fp=open("detailing.dat","rb")
        [databasename,host,user,password]=pickle.load(self.fp)

    return

As has been pointed out, there was a typo on self.fp. But here are a few other things that I notice that can cause problems.

First of all, you shouldn't be using the file constructor directly. You should instead use the built-in open function.

Secondly, you should avoid calling a file's close method outside a finally block. In this case, I've used python 2.6's with block. You can use this in Python 2.5 with the following command:

from __future__ import with_statement

This will prevent the file from being stuck open if an exception is thrown anywhere (as it will close the file when the with block is exited). Although this isn't the cause of your problem, it is an important thing to remember because if one of the file object's methods throws an exception, the file will get held open in sys.traceback indefinitely.

(note that you should probably accept Jarret Hardie's answer though, he caught the bug :-) )

Dyspeptic answered 31/3, 2009 at 13:48 Comment(1)
+1: much cleaner, and with good advice on the other issues in the snippetMincey
A
1

I got this error when I didn't chose the correct mode to read the file (wb instead of rb). Changing back to rb was not sufficient to solve the issue. However, generating again a new clean pickle file solved the issue. It seems that not choosing the correct mode to open the binary file somehow "damages" the file which is then not openable whatsoever afterward.

But I am quite a beginner with Python so I may have miss something too.

Authenticate answered 27/7, 2016 at 12:35 Comment(0)
P
0

While this is not a direct answer to the OP's question -- I happened upon this answer while searching for a reason for an EOFError when trying to unpickle a binary file with : pickle.load(open(filename, "r")).

import cPickle as pickle 
A = dict((v, i) for i, v in enumerate(words))
with open("words.pkl", "wb") as f:
    pickle.dump(A, f)



#...later open the file -- mistake:trying to read a binary with non-binary method
with open("words.pkl", "r") as f:
    A =pickle.load(f) # EOFError


# change that to
with open ("words.pkl", "rb") as f: # notice the "rb" instead of "r"
    A = pickle.load(f)
Panthia answered 13/7, 2016 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.