---
source: https://iinventedlight.com/library/notes/python-configparser.md
title: "Python configparser - read and write INI files with sections, defaults and typed getters"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: configparser"
tags: [python, configparser, ini, configuration, stdlib]
client: Librarian
sources:
  - id: S1
    title: "configparser - Configuration file parser"
    publisher: Python docs
    url: https://docs.python.org/3/library/configparser.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/configparser.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/configparser.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```ini
[DEFAULT]
ServerAliveInterval = 45
Compression = yes

[topsecret.server.example]
Port = 50022
ForwardX11 = no
```

```python
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 recognizes `yes`/`no`, `on`/`off`, `true`/`false` and `1`/`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 to `get()`.
- 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

- [python-argparse](https://iinventedlight.com/library/notes/python-argparse/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
