Python file I/O snippets should always include encoding='utf-8' to prevent silent data corruption on Windows
Summary
When Claude Code generates Python scripts that read or write text files using open(), it often omits the encoding parameter. On Windows, Python defaults to the system locale encoding (typically cp1252), not UTF-8. This causes silent mojibake when the files contain non-ASCII characters (accented letters, curly quotes, em dashes, etc.).
Reproduction
Claude Code generated this pattern for updating locale JSON files:
with open("messages/fr/parameters.json", "r") as f:
data = json.load(f)
# ... modify data ...
with open("messages/fr/parameters.json", "w") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
On Windows (cp1252 default):
open(..., "r")reads multi-byte UTF-8 sequences as cp1252 charactersjson.loadparses them into garbled Unicode stringsjson.dump+open(..., "w")writes those garbled strings back as UTF-8- Result:
é→é,—→â€", etc. — valid JSON, valid UTF-8, but wrong content
The corruption is invisible to prettier, eslint, and standard JSON validators. It only shows up when a downstream user reads the file and sees garbled text, or when a dedicated encoding-detection script runs.
Expected behavior
Generated Python file I/O should always include encoding="utf-8" explicitly:
import io
with io.open("messages/fr/parameters.json", "r", encoding="utf-8") as f:
data = json.load(f)
# ...
with io.open("messages/fr/parameters.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
Or with the built-in open:
with open("messages/fr/parameters.json", "r", encoding="utf-8") as f:
...
Impact
- Affects any Windows user who asks Claude Code to generate Python scripts that manipulate text files
- Particularly harmful for i18n/locale files, config files, source code with non-ASCII identifiers or comments
- The corruption is silent — tests pass, linters pass, the file is syntactically valid
Suggested fix
Add a system-level instruction (or CLAUDE.md guideline) that when generating Python code involving open() on text files, always include encoding="utf-8" as a keyword argument. This is already considered best practice in the Python community (PEP 597 introduced EncodingWarning to surface exactly this class of bug in Python 3.10+).
Environment
- OS: Windows 11 Pro
- Python: 3.x (default cp1252 locale)
- Claude Code version: current
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗