Python 3.2+configparserexception
configparser.DuplicateOptionError
exceptions
Raised by a strict parser when an option appears twice in a section while reading a single file, string or dictionary.
Syntax
except configparser.DuplicateOptionError as e: e.section, e.option, e.lineno
The trap is case: Host and host become one key after lowercasing, so that is a duplicate too. With strict=False there is no error and the last value silently wins. Setting parser.optionxform = str makes keys case-sensitive again.
Examples
import configparser
text = "[srv]\nHost = a\nhost = b\n"
try:
configparser.ConfigParser().read_string(text)
except configparser.DuplicateOptionError as e:
print(e.option, e.lineno) # → host 3
cs = configparser.ConfigParser()
cs.optionxform = str
cs.read_string(text)
print(dict(cs["srv"])) # → {'Host': 'a', 'host': 'b'}
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy