[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Handling files with CRLF line ending

From: Ángel
Subject: Re: Handling files with CRLF line ending
Date: Thu, 08 Dec 2022 20:34:42 +0100

On 2022-12-07 at 12:38 +0000, Chris Elvidge wrote:
> I don't use Python generally, but my understanding of it (only a
> quick test)
> f = open("demofile2.txt", "a")
> f.write("Now the file has more content!")
> f.close()
> f.write doesn't append either \r\n or \n unless specified.
> f.write("Now the file has more content!\n") does, in Windows, seem
> to 
> append \r\n. In Linux (Slackware) and WSL (Slackware), it only
> appends \n.
> So it does seem to be a "problem" with Windows rather than a problem 
> with Python.
> However, I may be wrong!!

The open() function in python has a newline parameter which can be
None, '', '\n', '\r', and '\r\n'.

When writing output to the stream, if newline is None (the default),
any '\n' characters written are translated to the system default line
separator (os.linesep).

Details on https://docs.python.org/3/library/functions.html#open

Thus, adding newline='\n' to your python open() calls would create the
files with unix newlines.

For bash itself, it's a bit more convoluted, as it will depend on the C
library used (which might be doing newline translation or not) that
willl be bridging between the POSIX API and Windows native functions.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]