On this page
shelve
Prev: selectors | Up: stdlib_module | Next: shlex
Rel: pickle, copyreg, marshal, dbm, sqlite3
Ref: docs.python.org — shelve
Tags: #public #python3 #stdlib
Import
import shelve
Overview
Persistent, dictionary-like key-value store backed by dbm files and pickle serialization. Use shelve for quick local persistence of arbitrary Python objects keyed by strings when setting up sqlite3 tables feels heavier than needed.
Quick Start
import shelve
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
db_path = str(Path(tmp) / "cache")
with shelve.open(db_path) as db:
db["user:1"] = {"name": "Ada", "roles": ["admin"]}
with shelve.open(db_path) as db:
assert db["user:1"]["name"] == "Ada"
Implementation Walkthrough
1. Mutating Nested Objects Safely
By default, shelve does not detect in-place mutations to mutable values (like db["tags"].append("new")). Either reassign the key explicitly or open with writeback=True (and call db.sync()):
import shelve
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
db_path = str(Path(tmp) / "prefs")
with shelve.open(db_path) as db:
db["tags"] = ["python"]
# Explicit read-modify-write avoids writeback memory overhead
tags = db["tags"]
tags.append("stdlib")
db["tags"] = tags
with shelve.open(db_path) as db:
assert db["tags"] == ["python", "stdlib"]
Gotchas & Boundaries
- Security (
pickleunder the hood): Never open an untrusted shelf file—reading keys unpickles data and can execute arbitrary code. - No concurrent multi-process writes:
shelveprovides no file locking or ACID transactions. For concurrent access or portable data files, use sqlite3. - Keys must be
str: Unlike Python dicts, integer or tuple keys raiseTypeError. - Platform-specific backend files: The underlying dbm backend may create
.db,.dat/.dir/.bak, or.sqlite3files depending on the OS and Python version, so shelf files are not safely portable across machines.
Common Functions
- shelve.open() —
open(filename, flag='c', protocol=None, writeback=False)— Open a persistent dictionary for reading and writing.
Classes & Objects
- shelve.BsdDbShelf — Shelf implementation using the "BSD" db interface. (Methods:
.clear(),.close(),.first(),.get(),.items(),.keys()) - shelve.DbfilenameShelf — Shelf implementation using the "dbm" generic dbm interface. (Methods:
.clear(),.close(),.get(),.items(),.keys(),.pop()) - shelve.Shelf — Base class for shelf implementations. (Methods:
.clear(),.close(),.get(),.items(),.keys(),.pop())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/bz2
- tuts/python3/stdlib_module/compression
- tuts/python3/stdlib_module/copyreg
- tuts/python3/stdlib_module/dbm
- tuts/python3/stdlib_module/gzip
- tuts/python3/stdlib_module/lzma
- tuts/python3/stdlib_module/marshal
- tuts/python3/stdlib_module/pickle
- tuts/python3/stdlib_module/selectors
- tuts/python3/stdlib_module/shelve/BsdDbShelf
- tuts/python3/stdlib_module/shelve/DbfilenameShelf
- tuts/python3/stdlib_module/shelve/open
- tuts/python3/stdlib_module/shelve/Shelf
- tuts/python3/stdlib_module/shlex
- tuts/python3/stdlib_module/sqlite3
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tarfile
- tuts/python3/stdlib_module/zipfile
- tuts/python3/stdlib_module/zlib
Related notes
- tuts/python3/stdlib_module/copyreg Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/dbm Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/marshal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pickle Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/selectors Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shelve/BsdDbShelf Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shelve/DbfilenameShelf Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shelve/open Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shelve/Shelf Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shlex Linked here; links to this page; shared tags: #stdlib