bsddb - mystery wrapped in enigma
Skip Montanaro
skip at pobox.com
Mon Oct 1 08:53:22 EDT 2001
d> I have been scouring the earth for a good tutorial on the bsddb
d> module. Is there anything (in print or otherwise) lurking about that
d> might help?
d> I am trying to extract information from an existing berkeley db file
d> (sleepycat).
Assuming the version of the file you want to probe matches the version of
libdb you have installed, and you know what type of file it is (hash, btree
or recno), you should be able to just open it with the proper open function
and then treat it more or less like a dictionary. To wit:
% file /etc/mail/aliases.db
/etc/mail/aliases.db: Berkeley DB (Hash, version 7, native byte-order)
% python
Python 2.1.1 (#6, Sep 24 2001, 09:14:11)
[GCC 3.0.1] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import bsddb
>>> dir(bsddb)
['__doc__', '__file__', '__name__', 'btopen', 'error', 'hashopen', 'rnopen']
>>> db = bsddb.hashopen("/etc/mail/aliases.db")
>>> db.keys()
['postmaster\x00', 'daemon\x00', 'ingres\x00', 'toor\x00', 'operator\x00', 'decode\x00', 'root\x00', '@\x00', 'mailer-daemon\x00', 'bin\x00', 'games\x00', 'nobody\x00', 'system\x00', 'uucp\x00', 'manager\x00', 'dumper\x00']
>>> for k in db.keys():
... print k, db[k]
...
postmaster root
daemon root
ingres root
toor root
operator root
decode root
root skip at pobox.com
@ @
mailer-daemon postmaster
bin root
games root
nobody root
system root
uucp root
manager root
dumper root
Note that the open function (hashopen) must match the type of the file (Hash
in this case).
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list