using shelve correctly?

Blair Hall b.hall at
Wed Dec 10 06:07:39 CET 2003

I am having trouble understanding the correct use of 'shelve'.

I have a number of small programs that read and write to a single
shelve file (not multithreading though).

It seems that by writing to an existing entry I
sometimes corrupt the database. For instance, I get the problem
when a Python script has opened the file and read and written a few 
objects it then tries to overwrite an existing entry:

cf = {}
FILE['correction factor'] = cf

The program then closes the file and terminates. Now if I go
back and look at the keys to the data base I find that
'correction factor' is no more and instead there is apparently
'correction factorr' (repetetion of last letter), however is
I try to use that key to access the data I get a KeyError.

I get the same pattern of corrupted key with other entries too (last 
letter repeats).

It appears that by deleteing the entry before hand

del FILE['correction factor']
FILE['correction factor'] = cf

the problem does not occur.
But I didn't think that this should be necessary.

Am I using shelve incorectly?

More information about the Python-list mailing list