Persistence and Serialization Modules

shelve

Provides persistent string-keyed mappings with pickle-serialized values over a DBM backend. By default, mutating an object retrieved from a shelf does not save that change unless it is reassigned. Close shelves reliably; do not open shelves from untrusted sources or assume concurrent writers are supported.

Formal Syntax & Structure

python syntaxpython
import shelve

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import shelvefrom tempfile import TemporaryDirectoryfrom pathlib import Pathwith TemporaryDirectory() as folder:    path = str(Path(folder)/"store")    with shelve.open(path) as db:        db["cart"] = ["tea"]    with shelve.open(path, flag="r") as db:        print(db["cart"])
Python Reference