EOFError in Python script

user46646 picture user46646 · Mar 31, 2009 · Viewed 40k times · Source

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?

Answer

Jarret Hardie picture Jarret Hardie · Mar 31, 2009

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