# pydoc > dbm

---
type: CommandReference
command: dbm
mode: pydoc
section: ""
source: pydoc3
---

## Quick Reference

- `d = dbm.open(file, flag, mode)` — open or create a database file
- `d[key] = data` — store data at key (overrides existing key)
- `data = d[key]` — retrieve data at key (raises `KeyError` if missing)
- `del d[key]` — delete data at key (raises `KeyError` if missing)
- `key in d` — test if key exists (`True` / `False`)
- `d.keys()` — return list of all existing keys (slow)
- `dbm.whichdb(file)` — guess which dbm submodule to use for a file
- `dbm.error` — tuple of `dbm.error` and `OSError`

## Name

Generic interface to all dbm clones.

## Synopsis

python
import dbm
d = dbm.open(file, flag='r', mode=0o666)
The returned object is a `dbm.gnu`, `dbm.ndbm` or `dbm.dumb` object, depending on the type of database being opened (determined by `whichdb()`). If the database does not exist and the `'c'` or `'n'` flag is used, the dbm type is chosen by module availability (tested in order: gnu, ndbm, dumb).

Key and data are always strings.

## Options

- `file` — path to the database file
- `flag` — access mode, one of:
  - `'r'` — read-only (default; fails if database does not exist)
  - `'w'` — read-write; fails if database does not exist
  - `'c'` — read-write; creates database if it does not exist
  - `'n'` — read-write; always creates a new database (overwrites existing)
- `mode` — numeric file mode (default `0o666`); used only when creating a new database

## Examples

Open a database for read-write, create if missing:

python
import dbm
d = dbm.open('mydb', 'c')
Store and retrieve data:

python
d['name'] = 'Alice'
print(d['name'])   # 'Alice'
Test for existence and delete:

python
if 'name' in d:
    del d['name']
List all keys:

python
keys = list(d.keys())
Guess the database type:

python
print(dbm.whichdb('mydb'))   # e.g. 'gnu' or 'ndbm'
## See Also

- [Python dbm module documentation](https://docs.python.org/3.10/library/dbm.html)
- Submodules: `dbm.dumb`, `dbm.gnu`, `dbm.ndbm`