pickle – putting more than 1 object in a file? [duplicate]

If you pass the filehandle directly into pickle you can get the result you want.

import pickle

# write a file
f = open("example", "w")
pickle.dump(["hello", "world"], f)
pickle.dump([2, 3], f)
f.close()

f = open("example", "r")
value1 = pickle.load(f)
value2 = pickle.load(f)
f.close()

pickle.dump will append to the end of the file, so you can call it multiple times to write multiple values.

pickle.load will read only enough from the file to get the first value, leaving the filehandle open and pointed at the start of the next object in the file. The second call will then read the second object, and leave the file pointer at the end of the file. A third call will fail with an EOFError as you’d expect.

Although I used plain old pickle in my example, this technique works just the same with cPickle.

Leave a Comment