shelve.open opens an existing shelf or creates its backing file when needed, returning a shelf object that behaves like a persistent dictionary. section 13.9
Use the returned shelf much like a dictionary, with these important differences:
- Assign, look up, delete, test membership, and inspect keys with ordinary dictionary-style operations. section 13.9
- Use only strings as keys; stored values can be structured objects such as tuples. section 13.9
- Keep data on disk rather than loading the entire shelf into memory, accessing file contents as needed. section 13.9
- Call close when the session ends so the shelf file is properly closed. section 13.9
Persistent address book
Open an address book, assign records by last name, close it, and reopen it later. A lookup such as book['flintstone'] returns the previously stored tuple, making the shelf act like a persistent dictionary. section 13.9
import shelve
book = shelve.open("addresses")
Start a shelf with import shelve and book = shelve.open("addresses"). section 13.9
