A shelf object is a persistent, file-backed mapping returned by shelve.open. It behaves like a dictionary while storing data on disk instead of keeping the entire dataset in memory. section 13.9
Use shelf objects like dictionaries, with a few important differences:
- Assign and retrieve values, delete entries, test membership, and call keys(). section 13.9
- Use only strings as keys, although stored values may be picklable Python objects. section 13.9
- Let shelve perform file access when needed, supporting fast lookups and datasets too large to fit in memory. section 13.9
- Close the shelf when finished so the persistent mapping can be reopened later. section 13.9
Persisting an address book
Open an address-book shelf, assign entries under string keys, close it, and reopen it in another session. Looking up book['flintstone'] returns the stored tuple. section 13.9
import shelve
book = shelve.open("addresses")
Start a shelf by importing shelve and opening a named file. section 13.9
