files-edit-tomllisted
Install: claude install-skill bitranox/bitranox-skills
# Edit TOML with a Python library, never by hand
## Overview
Round-trip TOML through a Python parser, then re-load to confirm it parses. Editing TOML as raw text
(sed/regex/manual) breaks the classic things: a value placed under the wrong table, a broken array, a
mangled quote - and worst, it churns or DROPS the comments and formatting that files like
`pyproject.toml` rely on. A library write is valid by construction; re-loading verifies it.
## Library - pick by whether comments must survive
- **`tomllib`** (stdlib, 3.11+) - **read only**, no writer. `tomllib.load(f)` (binary mode). The default
for reading.
- **`tomlkit`** - **the right tool for EDITING an existing file** (e.g. `pyproject.toml`): a style-
preserving round-trip that keeps comments, key order, and whitespace. Use this whenever the file has
comments or hand-formatting you must not lose.
- **`rtoml`** - fast read/write; **does NOT preserve comments**. Fine for machine-owned data files.
- **`tomli_w`** - minimal writer (`tomli` + `tomli_w`); also **drops comments**. Pre-3.11 read companion
is `tomli`.
See **bitranox:coding-python-use-modern-libraries** for the wider list. Reach for the structured editors
for the other formats too: **bitranox:files-edit-json**, **bitranox:files-edit-yml**,
**bitranox:files-edit-xml**.
## Pattern: edit an existing file (preserve comments) with tomlkit
```python
import tomlkit
from pathlib import Path
path = Path("pyproject.toml")
doc = tomlkit.parse(path.read_text(en