Opens or creates the DBM file named by filename (a string that can denote any path to a file, not just a name), and returns a suitable mapping object corresponding to the DBM file. When the DBM file already exists, open uses module whichdb to determine which DBM library can handle the file. When open creates a new DBM file, open chooses the first available DBM module in order of preference: dbhash, gdbm, dbm, and dumbdbm.

flag is a one-character string that tells open how to open the file and whether to create it, as shown in Table 11-1. mode is an integer that open uses as the file’s permission bits if open creates the file, as covered in Section 10.2.2 in Chapter 10. Not all DBM modules use flags and mode, but for portability’s sake you should always supply appropriate values for these arguments when you call

Table 11-1. flag values for



If file exists

If file does not exist



open opens the file

open raises error



open opens the file

open raises error



open opens the file

open creates the file



open truncates the file

open creates the file returns a mapping object m that supplies a subset of the functionality of dictionaries (covered in Chapter 4). m only accepts strings as keys and values, and the only mapping methods m supplies are m .has_key and m .keys. However, you can bind, rebind, access, and unbind items in m with the ...

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.