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?