I have created a shelve file and inserted a dictionary data. Now I want to cleanup that shelve file to reuse as a clean file.
import shelve
dict = shelve.open("Sample.db")
# insert some data into sample.db
dict = { "foo" : "bar"}
#Now I want to clean entire shelve file to re-insert the data from begining.
Easiest way to form a Shelf object is to use open() function defined in shelve module which return a DbfilenameShelf object. The filename parameter is assigned to the database created. Protocol parameter denotes pickle protocol writeback parameter by default is false. If set to true, the accessed entries are cached.
A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle.
A shelve in Python is a dictionary-like object. It is different from “dbm” databases as the value of shelve can be arbitrary Python objects.
dict.clear()
is the easiest, and should be valid, but seems to not actually clear the shelf files (Python 3.5.2, Windows 7 64-bit). For example, the shelf .dat
file size grows every time I run the following snippet, while I would expect it to always have the same size:
shelf = shelve.open('shelf')
shelf.clear()
shelf['0'] = list(range(10000))
shelf.close()
Update: dbm.dumb
, which shelve
uses as its underlying database under Windows, contains this TODO item in its code:
- reclaim free space (currently, space once occupied by deleted or expanded items is never reused)
This explains the ever-growing shelf file problem.
So instead of dict.clear()
, I'm using shelve.open
with flag='n'
. Quoting shelve.open()
documentation:
The optional flag parameter has the same interpretation as the flag parameter of dbm.open().
And dbm.open()
documentation for flag='n'
:
Always create a new, empty database, open for reading and writing
If the shelf is already open, the usage would be:
shelf.close()
shelf = shelve.open('shelf', flag='n')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With