I am writing a quiz program. I am trying to give the user the opportunity to write and add their own question. I have wrote functions to ask and add questions. I am trying to pickle the list of questions so I can auto load new questions anytime somebody adds one.
This is the code I am using to load the pickled file.
sciIn = open('sciList.txt','rb')
sci = pickle.load(sciIn)
sciIn.close()
I have this code in the function that adds questions.
sciOut = open("sciList.txt",'wb')
sci.append(dicQ)
pickle.dump(sci, sciOut)
sciOut.close()
When I run the code I get EOFError: Ran out of input which points to the loading of the pickle. I am not sure what I am doing wrong. I am using Python 3. Thanks for your help!
full code http://pastebin.com/HEp0KhRA
I think you might be doing the pickling right, but maybe working from an empty file at some point… and if you do this on the load, you can get odd
EOF
errors. Also, if you hand-edited the file in any way (or didn't store the data with pickle), then you can also getEOF
Errors on loading.This works (but note I'm storing questions as a dictionary).
From your code, it looks like you were pickling a list instead of a dictionary (hence the append to add the new question). The above should work for a list, as well, and you'd just append as you have done in your code. Regardless, if you are trying to read from an object from an empty file with pickle, you'll get an
EOF
Error -- however, if you start your code from a file that was created by pickling an empty list, you shouldn't get anEOF
Error.