I l@ve RuBoard Previous Section Next Section

10.6 The dbm Module

(Optional) The dbm module provides an interface to the dbm database handler (available on many Unix platforms). This is shown in Example 10-6.

Example 10-6. Using the dbm Module
File: dbm-example-1.py

import dbm

db = dbm.open("dbm", "c")
db["first"] = "bruce"
db["second"] = "bruce"
db["third"] = "bruce"
db["fourth"] = "bruce"
db["fifth"] = "michael"
db["fifth"] = "bruce" # overwrite
db.close()

db = dbm.open("dbm", "r")
for key in db.keys():
    print repr(key), repr(db[key])

'first' 'bruce'
'second' 'bruce'
'fourth' 'bruce'
'third' 'bruce'
'fifth' 'bruce'
    I l@ve RuBoard Previous Section Next Section