In this article, we will learn to create a shelf file, store data in a shelf file, retrieve the data that is stored in the shelf file, update the data of a shelf file, get a list of shelf keys, Delete data stored in a shelf file and closing shelf file.
The Shelve module is used to store data when using a relational database is not recommended. A shelf object is a dictionary-like object, which is defined in this module. Keys in shelf objects are strings and values are Python objects, that can be handled by the Pickle module. The shelve module comes with Python’s standard utility module, so there is no need to install it externally.
Classes in the Shelve Module
- Class Shelf: It is a subclass of collections.abc.MutableMapping which stores pickled values in the dictionary object. Functions present in the Shelf class get, close, and sync. These functions are covered below.
- Class BsdDbShelf: A subclass of Shelf that exposes first(), next(), previous(), last(), and set_location() which are available in the third-party bsddb module from pybsddb but not in other database modules. The dictionary object passed to the constructor must support those methods. Functions present in BsdDbShelf class are set_location, next, previous, first, and last.
- Class DbfilenameShelf: A subclass of Shelf which accepts a filename instead of a dict-like object. The underlying file will be opened using dbm.open(). By default, the file will be created and opened for both reading and writing. This class only contains a constructor.
Create a Shelf file using the “open” function
open(filename, flag = ‘c’): This function is used to create or open existing files using the shelve module.
- filename: The filename is assigned to the database file created.
- flag: It is an optional parameter. By default, its value is ‘c’ which means the file is opened for reading and writing. Flag ‘w’ is used to open for writing and Flag ‘r’ is used to open for reading.
Creating a shelf file
We use shelve.open (filename) to open a file and in case the file does not exist then it will create a new shelf file.
Python3
import shelve shelve_file = shelve. open ( "gfg" ) |
Using shelve.open() as a context manager:
Do not rely on the shelf being closed automatically. Always use close() or use shelve.open() as a context manager.
Python3
with shelve. open ( 'gfg' ) as f: pass |
Output:
Directory structure after running the above code:
Store the Data on the Shelf File
Create or open a shelf file. Then store the value on the shelve in the same way as we store it in a dictionary and, close the shelf file.
Python3
# import the Shelve module import shelve # create a shelf file shelve_file = shelve. open ( "gfg" ) # create a list of numbers num = [ 1 , 2 , 3 , 4 ] # Store num list in shelf file shelve_file[ 'num' ] = num # now, we simply close the shelf file. shelve_file.close() |
Output:
File content after storing data:
Retrieving Data Stored in the Shelf File
Open a shelf file then we will use a key to get data from the shelf as we do with a dictionary and, close the file.
Python3
# import the Shelve module import shelve # open a shelf file shelve_file = shelve. open ( "gfg" ) # get num list from shelf file num = shelve_file[ 'num' ] # now, we simply close the shelf file. shelve_file.close() # print num list print (num) |
Output:
[1, 2, 3, 4]
Updating Data Stored in the Shelf File
Open a shelf file then we will use a key to update data on the shelf as we do with a dictionary and, close the file.
Python3
# import the Shelve module import shelve # open a shelf file shelve_file = shelve. open ( "gfg" ) # print old data print (f "Old Data = {shelve_file['num']}" ) # update data shelve_file[ "num" ] = [ 11 , 22 , 33 , 44 ] # print updated data print (f "Updated Data = {shelve_file['num']}" ) # to make changes permanent shelve_file.sync() # now, we simply close the shelf file. shelve_file.close() |
Output:
Old Data = [1, 2, 3, 4] Updated Data = [11, 22, 33, 44]
File content after updating data:
Get a list of shelf keys using the “keys” function
Open a shelf file then use the keys() function as we do with a dictionary and, close the file.
Python3
# import the Shelve module import shelve # open a shelf file shelve_file = shelve. open ( "gfg" ) # print keys list print (f "Keys = {list(shelve_file.keys())}" ) # now, we simply close the shelf file. shelve_file.close() |
Output:
Keys = ['num']
Delete Data of Shelve File using Del Keyword
Open a shelf file. Then use ‘Python del‘ as we do with a dictionary. In last, close the file. To delete data stored at a key, we need to open a file using open() and then use “del”. This is the same as we do in a dictionary.
Python3
# import the Shelve module import shelve # open a shelf file shelve_file = shelve. open ( "gfg" ) # print keys before deleting print (f "Keys before deleting = {list(shelve_file.keys())}" ) # delete 'num' key del shelve_file[ "num" ] # print keys after deleting print (f "Keys after deleting = {list(shelve_file.keys())}" ) # now, we simply close the shelf file. shelve_file.close() |
Output:
Keys before deleting = ['num'] Keys after deleting = []
Close a Shelf File using the “close” Function
The close() is used to synchronize and close the shelf file. When we use the close function, all the changes made in the program are reflected in our shelf file.
Python3
# import the Shelve module import shelve # open a shelf file shelve_file = shelve. open ( "gfg" ) # now, we simply close the shelf file. shelve_file.close() |
Difference Between Shelve vs Pickle
The pickle module is used to convert Python objects into a byte stream. Python objects like lists, dictionaries, etc are supported by it. Whereas, The shelve module is built on top of the pickle module. It implements a serialization dictionary where keys are strings and values are pickled Python objects. Values can be only those objects that can be handled by the pickle module. The shelve module is used when we need to store data that is small in size and less complex. For large and complex data we use a database. It is insecure to load a shelf from an untrusted source because loading a shelf can execute arbitrary code.
Drawbacks of a pickle:
- Pickle objects are limited only to Python and can only be loaded in Python.
- The pickle file is unreadable.
Drawbacks of shelve:
- It does not support concurrent writes.
- It can not be used to store objects that can not be handled by pickle.