9.2.1 RawConfigParser Objects

RawConfigParser instances have the following methods:

defaults( )
Return a dictionary containing the instance-wide defaults.

sections( )
Return a list of the sections available; DEFAULT is not included in the list.

add_section( section)
Add a section named section to the instance. If a section by the given name already exists, DuplicateSectionError is raised.

has_section( section)
Indicates whether the named section is present in the configuration. The DEFAULT section is not acknowledged.

options( section)
Returns a list of options available in the specified section.

has_option( section, option)
If the given section exists, and contains the given option, return True; otherwise return False. New in version 1.6.

read( filenames)
Attempt to read and parse a list of filenames, returning a list of filenames which were successfully parsed. If filenames is a string or Unicode string, it is treated as a single filename. If a file named in filenames cannot be opened, that file will be ignored. This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user's home directory, and some system-wide directory), and all existing configuration files in the list will be read. If none of the named files exist, the ConfigParser instance will contain an empty dataset. An application which requires initial values to be loaded from a file should load the required file or files using readfp() before calling read() for any optional files:

import ConfigParser, os

config = ConfigParser.ConfigParser()
config.readfp(open('defaults.cfg'))
config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
Changed in version 2.4: Returns list of successfully parsed filenames.

readfp( fp[, filename])
Read and parse configuration data from the file or file-like object in fp (only the readline() method is used). If filename is omitted and fp has a name attribute, that is used for filename; the default is "<???>".

get( section, option)
Get an option value for the named section.

getint( section, option)
A convenience method which coerces the option in the specified section to an integer.

getfloat( section, option)
A convenience method which coerces the option in the specified section to a floating point number.

getboolean( section, option)
A convenience method which coerces the option in the specified section to a Boolean value. Note that the accepted values for the option are "1", "yes", "true", and "on", which cause this method to return True, and "0", "no", "false", and "off", which cause it to return False. These string values are checked in a case-insensitive manner. Any other value will cause it to raise ValueError.

items( section)
Return a list of (name, value) pairs for each option in the given section.

set( section, option, value)
If the given section exists, set the given option to the specified value; otherwise raise NoSectionError. While it is possible to use RawConfigParser (or ConfigParser with raw parameters set to true) for internal storage of non-string values, full functionality (including interpolation and output to files) can only be achieved using string values. New in version 1.6.

write( fileobject)
Write a representation of the configuration to the specified file object. This representation can be parsed by a future read() call. New in version 1.6.

remove_option( section, option)
Remove the specified option from the specified section. If the section does not exist, raise NoSectionError. If the option existed to be removed, return True; otherwise return False. New in version 1.6.

remove_section( section)
Remove the specified section from the configuration. If the section in fact existed, return True. Otherwise return False.

optionxform( option)
Transforms the option name option as found in an input file or as passed in by client code to the form that should be used in the internal structures. The default implementation returns a lower-case version of option; subclasses may override this or client code can set an attribute of this name on instances to affect this behavior. Setting this to str(), for example, would make option names case sensitive.

See About this document... for information on suggesting changes.