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
- The file consists of sections, each of which contains keys with values, similar to Windows INI files.
- Config parsers do not guess datatypes; values are stored internally as strings.
getboolean()is case-insensitive and recognizesyes/no,on/off,true/falseand1/0.- Keys are case-insensitive and stored in lowercase.
- Values in
[DEFAULT]apply to every section, and default values have precedence over fallback values passed toget(). - When you read several files, conflicting keys are taken from the most recent configuration.
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
- id: C1 claim: Config parsers store values as strings. source: S1 evidence: "always storing them internally as strings."
- id: C2 claim: getboolean is case-insensitive. source: S1 evidence: "This method is case-insensitive and recognizes Boolean values from"
- id: C3 claim: ConfigParser implements interpolation. source: S2 evidence: "ConfigParser -- responsible for parsing a list of"
Sources
- [S1] configparser - Configuration file parser. Python docs. https://docs.python.org/3/library/configparser.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/configparser.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/configparser.py . License: PSF-2.0. Accessed 2026-11-18.
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
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.},
}
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.
- python-configparser.table.json (table; CC-BY-4.0)
- python-configparser.schema.json (schema; CC-BY-4.0)
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.