Skip to content

Path.write_text does not check whether LF is preceded by CR before converting it to CRLF on windows, resulting in CRCRLF #143428

@DetachHead

Description

@DetachHead

Bug report

Bug description:

from pathlib import Path

path = Path("foo.txt")
path.write_text("foo\r\nbar")
print(path.read_bytes())  # b'foo\r\r\nbar'

i know i can workaround this behavior by passing newline="" to write_text or using write_bytes instead, but this should not be necessary. it should check whether the text is already using CRLF.

(sidenote: windows has supported LF for many years now. the automatic conversion many tools do by default is not only no longer necessary, but nowadays causes far more problems than it solves IMO. https://old.reddit.com/r/git/comments/7mg1q1/gits_crlf_handling_is_the_source_of_all_modern/)

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtopic-IOtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions