[Tutor] pickle problems

eryksun eryksun at gmail.com
Sun Aug 12 03:18:54 CEST 2012


On Sat, Aug 11, 2012 at 6:30 PM, Richard D. Moores <rdmoores at gmail.com> wrote:
>
> I wrote pickle_attempt.py as an exercise to try to learn to use the
> pickle module. See the version I edited for Tutor,
> pickle_attempt_for_web.py at
> <http://pastebin.com/SNwKRuSK>.
> ....
> But after closing the program and restarting it, those items have
> disappeared from D.

On line 68 you open the file in 'ab' mode. A pickle stream ends with
an ASCII period (\x2e). Anything appended after that is ignored. Use
'wb' mode. Also, Python 3.x defaults to protocol 3, which is binary,
so you might want a file extension other than .txt, such as .pkl,
.dat, .bin, etc.

If you're curious about the protocol, you can disassemble a pickle
using pickletools.dis(pickle), where pickle is bytes or a file-like
object.


More information about the Tutor mailing list