On this page
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
- Uses
pickleunder the hood: Never open untrusted shelf files, and never share concurrently across writers.
REPL Inspect
from shelve import open
help(open)
Linked from
Related notes
- tuts/python3/stdlib_module/shelve/BsdDbShelf Linked here; links to this page; shared tags: #shelve, #stdlib
- tuts/python3/stdlib_module/shelve/DbfilenameShelf Linked here; links to this page; shared tags: #shelve, #stdlib
- tuts/python3/stdlib_module/shelve/Shelf Linked here; links to this page; shared tags: #shelve, #stdlib
- tuts/python3/stdlib_module/shelve Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib