[Tutor] cannot save changes to dictionary in a shelve file

alan.gauld@bt.com alan.gauld@bt.com
Thu, 17 Oct 2002 10:30:16 +0100


>>> db["projects"] = {1:"proj001",2:"proj002",3:"proj003"}
>>> db["otherlist"] = (5,7,9,10)
>>> db.close()
>>> db.keys()
Traceback (most recent call last):

Because you closed db it no longer is a shelf so no 
longer has keys...

>>> db = shelve.open("projectsdb","c")
>>> db.keys()
['projects', 'otherlist']
>>> projects = db["projects"]

You now have created a local dictionary containing a copy of
the pone you shelved...

>>> projects[4] = "proj004"
>>> projects
{1: 'proj001', 2: 'proj002', 3: 'proj003', 4: 'proj004'}

You add a pair to the copy

>>> db.close()

But you didn't change the shelf!
You needed to do

db['projects'] = projects

to store your changes.

HTH,

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld