I was coding on Python, and I wanted to save some data using pickle, but I keep getting an EOF error, and I can't find the problem. Here's the code:
# Imports
import cPickle as pickle
I skipped a bit of stuff here
with open("SAVE_FILE.pickle") as file:
if file == None:
pickle.dump(0, file)
random_variable = pickle.load(file)
And this is my error message:
Traceback (most recent call last):
File "blablabla this is not important", line 172, in <module>
random_variable = pickle.load(file)
EOFError
Why does this happen? How can I fix it?
(random_variable is not the actual name of the variable)
Since you have kept the file handle opened, pickle will trying to load from the latest position of the file handle. You need to call file.seek(0)
to reset the file handle position, so pickle.load
will read from the beginning of the file handle.
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.