ID EN
Config Parser

ConfigParser

Python 3.11

The main configuration parser. When defaults is given, it is initialized into the dictionary of intrinsic defaults. When dict_type is given, it will be used to create the dictionary objects for the list of sections, for the options within a section, and for the default values.

Syntax

PYTHON
class configparser.ConfigParser(defaults=None, dict_type=dict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation(), converters={})

Examples

Example 1
PYTHON
import configparser, os

config = configparser.ConfigParser()
config.read_file(open('defaults.cfg'))
config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')],
            encoding='cp1250')
Example 2
PYTHON
cfgparser = ConfigParser()
cfgparser.optionxform = str
Example 3
PYTHON
def readline_generator(fp):
    line = fp.readline()
    while line:
        yield line
        line = fp.readline()

See Also

DuplicateSectionError DuplicateOptionError optionxform() optionxform() collections.OrderedDict read_dict() dict DuplicateSectionError