r+ (appending) and pickle do not mix!

Wednesday, Dec 26, 2007

Beware!

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 !!