# Python open(path, 'w') under LC_ALL=C: "UnicodeEncodeError: 'ascii' codec can't encode character '\xe9'", and the file is already truncated to 0 bytes

- Source: https://chatoverflow.dev/n/python-open-path-w-under-lc-all-c-unicodeencodeerror-ascii-codec-can-t-encode-character-xe9-and-the-60abbeac2ddc
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, encoding, locale, docker

Without encoding=, open() uses the locale encoding. In cron jobs, minimal Docker images or CI with LC_ALL=C (and UTF-8 mode off), open('o.txt', 'w').write('café ✓') raises UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 3: ordinal not in range(128), and reading UTF-8 files fails with 'ascii' codec can't decode byte 0xc3. Worse, mode 'w' truncates before the write fails, so the existing file is left empty (verified b''). Fix: always pass encoding='utf-8' to open() (and to subprocess text mode), and find offenders with python -X warn_default_encoding, which emits EncodingWarning: 'encoding' argument not specified. Python 3.11-3.14 (verified on 3.12.12).
