简体   繁体   中英

EOFerror in unpickling Python dictionary from an empty pickle file

Very newbie programmer, sorry if this is stupid or if my English is wrong. So, I have this command line address book that I am writing. It consists of a dictionary that holds an object with the key as the name variable, and each object has variables associated with it like the name of the person, the email, etc... It works, but now I'm trying to make it store the dictionary persistenly in memory using pickle.

def create_person():
    """Adds an instance object of the Person class to the dictionary persons. persons is a global variable, that has been created previously. DATA is a variable that points to a file named test.data that exists in the same directory as the script."""
    name = raw_input("Enter the person's name here: ")
    email = raw_input("Enter the person's email here: ")    
    phone = raw_input("Enter the person's phone here: ")
    address = raw_input("Enter the person's address here: ")
    f = open(DATA, "rb")
    persons = pickle.load(f) #assign whatever is saved in the dictionary in persistent memory to global variable persons, which is empty at this point in the beginning
    f.close()
    persons[name] = Person(name, email, phone, address)    
    f = open(DATA, "wb")
    pickle.dump(persons, f)
    f.close()

However, I'm getting this error:

Traceback (most recent call last):
File "testpickle.py", line 85, in <module>
  main()
File "testpickle.py", line 40, in main
  create_person()
File "testpickle.py", line 20, in create_person
  persons = pickle.load(f)
File "/home/pedro/anaconda/lib/python2.7/pickle.py", line 1378, in load
  return Unpickler(file).load()
File "/home/pedro/anaconda/lib/python2.7/pickle.py", line 858, in load
  dispatch[key](self)
File "/home/pedro/anaconda/lib/python2.7/pickle.py", line 880, in load_eof
  raise EOFError
EOFError

I don't understand this. I had actually already written this program, and it was working with memory saving, but I accidently deleted it. What is happening?

pickle hitting EOF is a sure tell the file is corrupt (probably truncated, maybe a write operation failed).

If you upload it somewhere, we might be able to deduce what exactly is wrong with it. In the worst case, you'll have to peek inside and deduce the data that was there to re-input it by hand.

That's your price for using an unreadable format and not paying attention to its integrity (eg writing to another file and only moving it over the original one after saving succeeded ).

UPDATE:

If you want to start from a new, "empty" file, then handle the case when the file is missing and produce an empty dict . After all, the file is supposed to be missing initially, isn't it?

An empty file is not valid pickle data (it has to at least contain information about the object's type).

Here's "one obvious way" ((c) Python Zen) to handle a missing file:

import errno    
try: f = open(DATA, "rb")
except IOError,e:
    if e[0]==errno.ENOENT: persons={}
    else: raise
else:
    persons = pickle.load(f)
    f.close()
    del f

I don't see anything obviously wrong with your code, but if you just want to store key/value pairs like this, you probably ought to use the shelve module instead of a home-brewed solution.

import shelve

persons = shelve.open(DATA)

def create_person():
    """Adds an instance object of the Person class to the dictionary persons. persons is a global variable, that has been created previously. DATA is a variable that points to a file named test.data that exists in the same directory as the script."""
    name = raw_input("Enter the person's name here: ")
    email = raw_input("Enter the person's email here: ")    
    phone = raw_input("Enter the person's phone here: ")
    address = raw_input("Enter the person's address here: ")
    persons[name] = Person(name, email, phone, address)    

while more_people:
    create_person()

persons.close()    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM