so.very.tired so.very.tired - 5 months ago 28
Python Question

Python: Update serialized object

I am trying to perform a simple task:

1. De-serialize a previously serialized object

2. Updating this object

3. Serializing it back for later use

I tried to do it with

with no luck.

I start by doing this:

empty_list = []
f = open('backup.p', 'wb')
pickle.dump(empty_list, f)

and later:

f = open('backup.p', 'rb+')
l = pickle.load(f)
pickle.dump(l, f)

But when I try to load again the supposedly updated list:

f = open('backup.p', 'rb')
updated_list = pickle.load(f)
print(updated_list) # prints [] instead of ['string']

Why doesn't the second call to
overwrite the content of
, with the new list
? Do I have to remove
in order to get the desired behavior?


After this:

f =  open('backup.p', 'rb+')
l = pickle.load(f)

you've positioned the file object f at a point in the file after the pickle of empty_list. That means when you dump another object to the file:

pickle.dump(l, f)

the new pickle gets written after the first pickle. You need to avoid that, either by clearing the file before dumping the new pickle:

or by dumping to a new file and then replacing the original file with the new one. (You could also seek, dump, then truncate at the end to clear any trailing garbage instead of going seek, truncate, dump.)