Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make shelve file empty in python?

Tags:

python

shelve

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.
like image 858
Abhishek Kulkarni Avatar asked Jun 27 '13 11:06

Abhishek Kulkarni


People also ask

How do I use shelve module in Python?

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.

What is a shelf file Python?

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.

Is shelve a database?

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.


1 Answers

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')
like image 60
george Avatar answered Oct 26 '22 07:10

george