r+ (appending) and pickle do not mix!

Published: Wednesday, Dec 26, 2007 Last modified: Monday, Jul 15, 2024


If you open a file with r+:

f=open('/tmp/pickletest', 'r+')

and then load from it:

dict = pickle.load(f)

and then write to it:

pickle.dump(dict, f)

You will have two pickles in that file, and only the last pickle will have what you want. Hence appending and pickling DO NOT MIX !!