so.very.tired so.very.tired - 3 months ago 13
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

pickle
with no luck.

I start by doing this:

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


and later:

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


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']
f.close()


Why doesn't the second call to
dump()
overwrite the content of
backup.p
, with the new list
['string']
? Do I have to remove
buckup.p
in order to get the desired behavior?

Answer

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:

f.seek(0)
f.truncate()

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.)

Comments