On this page
  1. shelve.open
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

shelve.open

Prev: shelve.DbfilenameShelf | Up: shelve | Next: shelve.Shelf
Rel: shelve.BsdDbShelf, shelve.DbfilenameShelf, shelve.Shelf
Ref: docs.python.org — shelve.open
Tags: #public #python3 #stdlib #shelve


Import

import shelve
from shelve import open

Signature & Overview

shelve.open(filename, flag='c', protocol=None, writeback=False)

Open a persistent dictionary for reading and writing.

Usage

from pathlib import Path
import shelve, tempfile

with tempfile.TemporaryDirectory() as tmp:
    p = str(Path(tmp) / 'cache')
    with shelve.open(p) as db:
        db['user'] = {'name': 'Ada'}
    with shelve.open(p) as db:
        assert db['user']['name'] == 'Ada'

Gotchas

REPL Inspect

from shelve import open
help(open)

Linked from

Related notes