Python Glossary Русский

Python 3.14+configparserexception

configparser.InvalidWriteError

exceptions

Raised by write() when the written text would be parsed differently on the next read — for example, a key starting with '[' would look like a section header.

Syntax

except configparser.InvalidWriteError: ...

The error appears on write, not on set(): you can assign such a key but not save it. Before 3.14 write() silently produced a file that read back with a different structure. Validate keys that come from user input before saving.

Examples

import configparser, io
cfg = configparser.ConfigParser()
cfg["s"] = {"[x]": "1"}  # присвоить можно
try:
    cfg.write(io.StringIO())
except configparser.InvalidWriteError as e:
    print(e)  # → Cannot write key [x]; begins with section pattern

See also

Open in the full glossarydocs.python.orgReport an inaccuracy