cPickle and HIGHEST_PROTOCOL problems

Tim Peters tim.one at comcast.net
Tue Feb 24 16:00:06 CET 2004

[Simon Dahlbacka]
> I'm trying to cache some information using cPickle
> and I thought that using cPickle.HIGHEST_PROTOCOL would give me an
> efficient representation.
> However,
> when trying to load the information back I just keep getting a broken
> pickle with EOF error from cPicle.load(file('myfile.pickle','r'))

Change that 'rb'.  Also make sure that the file you write the pickle to is
opened with 'wb'.

> if I am not using cPickle.HIGHEST_PROTOCOL  or -1 or 2 it seems to
> work nicely, is this a bug or something else?  (python 2.3.2 on winxp)

Windows distinguishes between text-mode and binary-mode files.  Pickles are
binary data, so files containing pickles must always be opened (for both
writing and reading) in binary mode.  "Mysterious errors" are expected if
you don't.

More information about the Python-list mailing list