On this page
dbm
Prev: datetime | Up: stdlib_module | Next: decimal
Rel: pickle, copyreg, shelve, marshal, sqlite3
Ref: docs.python.org — dbm
Tags: #public #python3 #stdlib
Import
import dbm
Overview
Generic interface to lightweight, disk-backed key-value databases (dbm.sqlite3, dbm.ndbm, dbm.gnu, and pure-Python fallback dbm.dumb). Use dbm when you need fast persistent bytes/str key-value lookups without SQL schema design or pickle overhead.
Quick Start
import dbm
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
path = str(Path(tmp) / "store")
with dbm.open(path, "c") as db:
db["token"] = "abc123"
assert db["token"] == b"abc123" # values are always returned as bytes
Implementation Walkthrough
1. Opening Modes and Inspecting the Backend
dbm.open(file, flag="r") supports "r" (read-only), "w" (read/write existing), "c" (read/write, create if missing), and "n" (always create a fresh empty database). dbm.whichdb(path) reports which backend created an existing file:
import dbm
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
path = str(Path(tmp) / "kv")
with dbm.open(path, "n") as db:
db[b"k1"] = b"v1"
db[b"k2"] = b"v2"
keys = set(db.keys())
assert keys == {b"k1", b"k2"}
assert dbm.whichdb(path) is not None
Gotchas & Boundaries
- Strings are accepted on write,
bytesare returned on read:db["a"] = "1"works, butdb["a"]returnsb"1". Encode/decode explicitly (e.g., UTF-8 or json bytes) for structured data. - No built-in concurrency locking: Simultaneous writers across processes can corrupt
dbm.dumb/dbm.ndbmdatabases. Use sqlite3 directly when you need transactions or multi-process safety. - Backend portability: Different platforms default to different backends (
dbm.sqlite3in Python 3.13+,dbm.ndbmon macOS/BSD,dbm.gnuon Linux); don't assume a raw DBM file copied across OSes will open with the same backend.
Common Functions
- dbm.open() —
open(file, flag='r', mode=438)— Open or create database at path given by file. - dbm.whichdb() —
whichdb(filename)— Guess which db package to use to open a db file.
Constants & Attributes
dbm.error(tuple) =(<class 'dbm.error'>, <class 'OSError'>)
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/datetime
- tuts/python3/stdlib_module/dbm/open
- tuts/python3/stdlib_module/dbm/whichdb
- tuts/python3/stdlib_module/decimal
- tuts/python3/stdlib_module/gzip
- tuts/python3/stdlib_module/lzma
- tuts/python3/stdlib_module/marshal
- tuts/python3/stdlib_module/pickle
- tuts/python3/stdlib_module/shelve
- 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/datetime Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/dbm/open Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/dbm/whichdb Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/decimal 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/shelve Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib