Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Shelve Module
Next article icon

Python Shelve Module

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Shelve module in Python
 

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:

Shelve module in Python
 

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:

Shelve module in Python
 

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.

Next Article
Python Shelve Module
author
06akshay2002
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Technical Scripter 2022
Practice Tags :
  • python

Similar Reads

    Python Modules
    Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
    7 min read
    Reloading modules in Python
    We are given a case where we want to update and test a Python module without restarting the interpreter. This is especially helpful during development when you're modifying module files externally and want those changes to reflect immediately. Python allows us to reload a previously imported module
    3 min read
    Python Module Index
    Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
    4 min read
    Where Does Python Look for Modules?
    Modules are simply a python .py file from which we can use functions, classes, variables in another file. To use these things in another file we need to first import that module in that file and then we can use them. Modules can exist in various directories. In this article, we will discuss where do
    3 min read
    Relative Import in Python
    In Python, relative imports allow us to import modules in relation to the current module's location within a package structure. This means instead of using full paths, we can import modules using . (current directory) or .. (parent directory), making the code more concise and easier to maintain. Rel
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences