[Tutor] how to create a persistent dictionary w/ cpickle?

Alan Gauld alan.gauld at btinternet.com
Thu Sep 9 02:17:12 CEST 2010

"Carter Danforth" <carter.danforth at gmail.com> wrote

> Hi, I'm trying to a create a basic addressbook for practice. I'm 
> using a
> dictionary with cpickle, though I'm not sure how to persistently 
> store each
> instance in the dictionary. Below is the code I have so far.

If you use a dictionary it makes more sense to use shelve rather than 
pickle for storage.
Shelve uses pickle under the hood but it makes a file look like a 
dictionary so you
don't need to load all the data and store it all again as you would 
with pickle.

Unfioortunately your code appears to be truncated, however...

> import cPickle as p
> addressbook = 'addressbook.data'
> f = file(addressbook, 'r+')

I think you should use a binary file for pickle so the mode should be 
'rb' not 'r+'


Alan G 

More information about the Tutor mailing list