pickle, cPickle, & HIGHEST_PROTOCOL
tim.peters at gmail.com
Sun Jan 30 17:57:37 EST 2005
> I wonder if someone can explain what is wrong here. I am pickling a
> list of dictionaries (see code attached) and unpickling it back using
> the HIGHEST_PROTOCOL of pickle and cPickle.
> ... on Win98.
Pickles are binary data. Therefore you should open pickle files in
binary mode on all platforms, and must open them in binary mode on
Windows. That is, replace your lines:
f = file('pkl_' + fn, 'w')
f = file('pkl_' + fn, 'r')
f = file('pkl_' + fn, 'wb')
f = file('pkl_' + fn, 'rb')
It's true that you can get away with opening pickle files in text mode
on Windows if you stick to the default pickle protocol (0), although
in that case your pickle files won't be portable across platforms.
For that reason, don't try to be clever: always use binary-mode files
to store pickles, regardless of pickle protocol in use.
More information about the Python-list