Shelve in Python

shelve in python

What is Shelve?

In simple words, We can save variables in our python programs to binary shelf files. So, Shelve in Python is used as a simple persistent storage for the arbitrary Python objects which can be pickled, using a dictionary-like API.
We use shelve module in Python, that helps to implement the persistent storage. It creates a file similar to dbm database on UNIX like systems. Only string data type can be used as key in this special dictionary object, whereas any pickle able object can serve as value. So, the shelve is accessed by key, just like within a dictionary.

Creating a new Shelve :

We will simply to use shelve is via the Dbfilenameshelf class. It uses anydbm class to store the data. Or simply, we can use function.

For first, we will import our shelve module

import shelve

To form a Shelf object, easiest way is to use open() function defined in shelve module which return a DbfilenameShelf object.

open(filename, flag = ‘c’, protocol=None, writeback = False)
The filename parameter is assigned to the database created.

Default value for flag parameter is ‘c’ for read/write access, ‘w’ (write only) ‘r’ (read only) and ‘n’ (new with read/write).
Protocol parameter denotes pickle protocol and write-back parameter by default is false. If write back is set to true, the accessed entries are cached.
Now, we will use open() function to simple open a .db binary file. Name of the can be of our own choice, so for instance im using data_shelve as name of my file.
data =‘data_shelve.db’)
Further now, we are going to give the key along with values using our variable name. Lets say we are putting the information about a person, putting his name, age and email address.

data[‘Name’] = “John Wick”
data[‘Age’] = 32
data[‘Email’] =

The data has been written in the shelve file. Now, we will close the file which we have opened by using the close() function.


Closing a file is important for following reasons:

  • If a file is opened to perform any operations then it’s locked to be opened by any other resource until the process itself closes it.
  • Operating System keeps a check on the number of files opened by a program and thus closing files after use allows you stay within that restriction.

Now to access value of a particular key in shelve file. We will open the file again

data =‘data_shelve.db’)
After opening the file we can access any key.
so the output will be like:
John Wick
Which is the name we put inside our shelve file.
Same like this we can access any key we want to. For instance:
and the outputs will be the values that we put.
Simply we can use the functions of dictionary over here because the data we stored is in a dictionary like data structure.
We can use items(), keys() or values() over here.
so they will provide the results as:
[(‘Name’, ‘John Wick’), (‘Age’, 32), (‘Email’, ‘
[‘Name’, ‘Age’, ‘Email’]
[‘John Wick’, 32, ‘’]
Some more functions like update() can be use to add keys and values to our shelve file.
Conclusion :
We learned about shelve module which provides convenient mechanism for storing persistent dictionary object.



Codec Networks is a global provider of end-to-end Information Security Services, Robust Solutions and Technology Products to clients in various industry verticals.

View all posts by

Leave a Reply

Your email address will not be published. Required fields are marked *