In Python shelve you access the keys randomly. In order to access the keys randomly in python shelve we use
open() function. This function works a lot like the file open() function in File handling. Syntax for open the file using Python shelve
shelve.open(filename, flag='c' , writeback=True)
In Order to access the keys randomly in shelve in Python, we have to take three steps:
- Storing Python shelve data
- Retrieving Python shelve data
- Updating Python shelve data
Storing Python shelve data :
In order to store python shelve data, we have to create a file with full of datasets and open them with a
open() function this function open a file which we have created.
Retrieving Python shelve data :
After storing a shelve data, we have to retrieve some data from a file in order to do that we use index operator  as we do in lists and in many other data types.
['bared_to_you', 'The_fault_in_our_stars', 'The_boy_who_never_let_her_go']
Note : Output will be depend on what you have store in a file
Updating Python shelve data :
In order to update a python shelve data, we use append() function or we can easily update as we do in lists and in other data types. In order to make our changes permanent we use
Enter the number of values 5 Enter the value Who moved my cheese? Enter the value Our impossible love Enter the value Bourne Identity Enter the value Hush Enter the value Knock-Knock
['bared_to_you', 'The_fault_in_our_stars', 'The_boy_who_never_let_her_go', 'Who moved my cheese?', 'Our impossible love', 'Bourne Identity', 'Hush', 'Knock-Knock']
Note : Input and Output depend upon user, user can update anything in a file which user want according to user input, output will be changed.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Subclass and methods of Shelve class in Python
- Retrieving File Data From HDFS using Python Snakebite
- Retrieving HTML From data using Flask
- Python | Check if a list is contained in another list
- Python | setting and retrieving values of Tkinter variable
- Django ORM - Inserting, Updating & Deleting Data
- Python IMDbPY – Retrieving person using person ID
- Python IMDbPY – Retrieving company using company ID
- Python IMDbPY - Retrieving movie using movie ID
- Python IMDbPY – Retrieving art department cast from the movie object
- Python IMDbPY - Retrieving actor from the movie details
- Python IMDbPY - Retrieving role played by actor from the movie details
- Retrieving Cookies in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- PyQt5 - Retrieving caption of Check Box
- PyQt5 Scrollable Label - Retrieving tooltip text
- PyQt5 QSpinBox - Retrieving Description
- Merging and Updating Dictionary Operators in Python 3.9
- Python | Updating value list in dictionary
- PyQt5 QCalendarWidget - Updating it
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.