Using shelve

https://docs.python.org/3/library/shelve.html

INTRODUCTION

A 'shelf' is a persistent, dictionary-like object. The values (not the keys!) in a shelf can be essentially arbitrary Python objects - anything that the 'pickle' module can handle.


# shelve.open(filename, flag='c', protocol=None, writeback=False)
# Open a persistent dictionary.

# Shelf.sync()
# Write back all entries in the cache if the shelf was opened with 'writeback'
# set to True. Also empty the cache and synchronize the persistent
# dictionary on disk, if feasible.
# This is called automatically when the shelf is closed with close().

# Shelf.close()
# Synchronize and close the persistent dict object.
# Operations on a closed shelf will fail with a ValueError.

import shelve

adict = {"a": "jeden", "b": "dwa"}
alist = [1, "a", 2, "b"]

db = shelve.open("data.shelve")   # opening Shelf

db["adict"] = adict
db["alist"] = alist
assert len(db) == 2
assert "alist" in db

db.close()   # closing Shelf