ConfigParser

Python has an excessive number of file parsers, especially given the PyPI. But there is just one standard library package that offers a configuration parser--configparser. The syntax expected by this parser is a conglomeration of many standards, and the parser has several large opt-in features.

Syntax and Syntax Errors

Sections

All options must be set under a section header. Section headers cannot be repeated, or the parser will raise configparser.DuplicateSectionError.

[Section1]
option=1

[Section2]
option=2

Options

Options and values are parsed as strings. The Option and value is delimited by either equals signs (=) or colonos (:). Spaces around the delimiter are ignored, but all other spaces are retained in the option and value. Options must be set with a value, even if it is empty.

[Section]
foo=1
bar=abc
baz=

If the parser is created with allow_no_value=True, options without values are allowed. When gotten, they return None. Otherwise, these options will raise configparser.ParsingError.

[Section]
keyword-instruction

Options must be unique within a section. Repeated settings will raise configparser.DuplicateOptionError.

Default Values


CategoryRicottone