shelve vs pickle

Sheila King sheila at
Sat Sep 15 06:59:43 CEST 2001

On Fri, 14 Sep 2001 20:28:50 -0000, hungjunglu at wrote in
comp.lang.python in article
<mailman.1000499358.27006.python-list at>:

:(3) Or is the advantage of Shelve in the open() and close() 
:statements? Is it smart enough to save only those items that have 
:been modified? So that open() and close() are fast enough if only a 
:few items have been touched?
:(4) In another test, I closed the Python program while it was in a 
:loop writing items to shelve. The database file got corrupted enough 
:that the majority of items are now missing.
:All in all, I am just wondering, just like some other people have 
:asked before in the newsgroup (and not getting any real answer): when 
:is it good to use shelve?

I really liked your questions. Right now, I was working with some
programs that use shelve. It is promoted rather highly in a number of
the Python books that I have purchased, so I just thought it was a good
thing, and I would use it. But you have very interesting question. (more

:I guess shelve can only be used safely if extra caution is taken into 
:(1) Before opening a shelve file, make sure of making a back up copy. 
:(This can be done at the moment of saving, too, depends on each 
:person's preference.)
:(2) Delete the back up copy only if the shelve has been closed 
:(3) Close the shelve file often, to make sure that your changes are 

Here's something I noticed with shelve tonight:

I have a shelve database, and I delete all of the entries from it. When
I ask it to print the keys() for the database, I get an empty list, as I
would expect. However, if I view the contents of the database file, I
can see that all the entries are still there, and that in fact it is
taking up 11K of space. How can I compact those empty entries out to
regain disk space?

Also, when I use whichdb to guess the type of database, it tells me that
it is a dbhash. However, I'm using Python on a Win98 machine, and I've
not installed the Sleepy Cat database on it. I was really surprised by
that, too. I would have thought that it would have been the dumbdbm

Sheila King

More information about the Python-list mailing list