Python Glossary Русский

all Python 3configparserexception

configparser.ParsingError

exceptions

Raised when file lines cannot be parsed as a section, an option or a comment. The list of all bad lines is in the errors attribute.

Syntax

except configparser.ParsingError as e: e.source, e.errors

The parser does not stop at the first bad line: it reads the whole file, and the options already parsed stay in the parser. So after catching ParsingError the config is partially filled, and using it further is risky. A line without = or : is an error unless allow_no_value=True.

Examples

import configparser
cfg = configparser.ConfigParser()
try:
    cfg.read_string("[a]\nx = 1\njust text\nmore text\n")
except configparser.ParsingError as e:
    print(len(e.errors))  # → 2 — все плохие строки сразу
print(dict(cfg["a"]))  # → {'x': '1'} — разобранное осталось

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy