Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Python ConfigParser: read literal percent signs without hidden interpolation

Last updated: 1 Oct 20265 min read
tutorial
IntermediateBy AITrove Editorial

ConfigParser interpolation and case folding can change how a stored option is read.

Download Python source kit

Operation contract

A controlled export configuration contains a literal percent sign. With interpolation disabled, the application reads those bytes as text. A second configuration repeats the same option with different letter case; strict parsing rejects that duplicate before it can replace the first value.

Failure boundary

Disabling interpolation does not validate business values, protect secrets, or decide which configuration file is trusted. ConfigParser stores string values; parse and range-check each field before publishing settings to workers.

Working program

python
import configparser

settings = configparser.ConfigParser(interpolation=None, strict=True)
settings.read_string("[export]\npattern = paid%complete\nretention_days = 47\n")
print("pattern", settings["export"]["pattern"])
print("retention", settings.getint("export", "retention_days"))

duplicate = configparser.ConfigParser(interpolation=None, strict=True)
try:
    duplicate.read_string("[export]\nRetention = 47\nretention = 73\n")
except configparser.DuplicateOptionError:
    print("case_duplicate_rejected", True)

Output

Output
pattern paid%complete
retention 47
case_duplicate_rejected True

Costs and limits

Configuration parsing allocates values proportional to the source text. The input file still needs an ownership and size policy before parsing.

Common Mistakes

  • Default interpolation treats percent syntax as special.
  • Option names are case-insensitive by default, which affects duplicate detection.
  • getint converts text but does not enforce a domain range.

Connected lessons

Test this contract.

python
configparser-literal-percent
Storage details