all Python 3configparserexception
configparser.DuplicateSectionError
exceptions
Raised when a section with that name already exists — on add_section() or when it repeats within one file.
Syntax
except configparser.DuplicateSectionError as e: e.section, e.source, e.lineno
Strict mode is on by default (strict=True), so a config that relied on merging repeated sections now fails on read. strict=False merges the repeats, and the last value wins for duplicate keys. Repeating a section across separate files read one after another is not an error.
Examples
import configparser
text = "[a]\nx = 1\n[a]\nx = 2\n"
try:
configparser.ConfigParser().read_string(text)
except configparser.DuplicateSectionError as e:
print(e.section, e.lineno) # → a 3
loose = configparser.ConfigParser(strict=False)
loose.read_string(text)
print(loose["a"]["x"]) # → 2
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy