← All notes

Python configparser - read and write INI files with sections, defaults and typed getters

Answer: The ConfigParser class implements a basic configuration language with a structure similar to Windows INI files, and you can treat a config parser much like a dictionary. For non-string values, config parsers provide handy getter methods for integers, floats and booleans.

Example

[DEFAULT]
ServerAliveInterval = 45
Compression = yes

[topsecret.server.example]
Port = 50022
ForwardX11 = no
import configparser

config = configparser.ConfigParser()
config.read(["defaults.ini", "override.ini"])   # later files win

top = config["topsecret.server.example"]
port = top.getint("Port")                 # 50022
x11 = top.getboolean("ForwardX11")        # False
interval = top["ServerAliveInterval"]     # '45' from [DEFAULT]
cipher = top.get("Cipher", "3des-cbc")    # fallback value

config["new"] = {"key": "value"}
with open("example.ini", "w") as configfile:
    config.write(configfile)

Rules

Interpolation

| Class | Syntax | |---|---| | BasicInterpolation (the default) | %(home_dir)s/lumberjack refers to another value in the same section or [DEFAULT]; write %% for a literal percent | | ExtendedInterpolation | ${section:option} refers to a value from a foreign section |

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, configparser - Configuration file parser; python/cpython (GitHub), Lib/configparser.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, configparser - Configuration file parser; python/cpython (GitHub), Lib/configparser.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:mjn8278w

iinventedlight. 2026. "Python configparser - read and write INI files with sections, defaults and typed getters." iinventedlight Library, iil:note:mjn8278w, version of 2026-11-18. https://iinventedlight.com/library/notes/python-configparser/. License: CC-BY-4.0.

@misc{iil_note_mjn8278w,
  author = {{iinventedlight}},
  title = {{Python configparser - read and write INI files with sections, defaults and typed getters}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/python-configparser/},
  note = {Permanent ID iil:note:mjn8278w. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-configparser.md.