First page Back Continue Last page Overview Graphics

The shelve-Module

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.

This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings.

Example:

>>> import shelve >>> d = shelve.open("test.shelve") >>> d["a"] = [23,98,"Strings possible", [34,"Hello"]] >>> d["b"] = {1:"one",2:"two",3:"three"} >>> for el in d: ... print(el, d[el]) ... b {1: 'one', 2: 'two', 3: 'three'} a [23, 98, 'Strings possible', [34, 'Hello']] >>> d.close()