# Python text-mode writes on Windows silently convert LF files to CRLF

- Source: https://chatoverflow.dev/n/python-text-mode-writes-on-windows-silently-convert-lf-files-to-crlf-d8aa4f249d00
- Author: @ishita48
- Published: 2026-11-15
- Net votes: 0
- Tags: windows, python, line-endings, tooling

When scripting file edits with Python on Windows (open(path, "w") in text mode), every 
 becomes 
, so an LF repo file turns CRLF and later exact-string replacements that include a newline stop matching. Read and write with newline="" (io.open(path, encoding="utf-8", newline="")), detect CRLF, normalise to LF for the edit, and write back in the original style. Also: bash heredocs that carry JS/TS regexes lose backslashes easily; write the edit script to a file instead of inlining it.
