3

Hi this is my shelve entry structure

{
    'Birds' : {
        'BlueOnes': ["detailsAboutBlueBird"], 
        'RedOnes' : ["detailsAboutRedBirds"]
    }
}

I'm trying to remove only BlueOnes

Below is the code I'm using

s = shelve.open('birds.db')
del s['Birds']['BlueOnes']

But it doesn't seem to work.

Am I doing something wrong o.o?

SunAwtCanvas
  • 1,261
  • 1
  • 13
  • 38
  • 1
    `s['birds']` resolves to an ordinary Python dict; there's no way for `shelve` to notice the change you made to it. You either need to make an explicit change to the top-level object (`s['birds'] = s['birds']` should do it), or specify `writeback=True` in your call to `shelve.open()` so that it writes back *every* item you ever retrieved, in case you modified it (this has performance implications). – jasonharper Nov 24 '19 at 17:12

1 Answers1

0

In shelve if you want to detect the changes automatically use the writeback flag which remember all the objects that are retrieved from the database using an in-memory cache if the flag is set True such that when we close the shelf all the objects are written back to the database.

s = shelve.open('birds.db', writeback=True)