bug-bash
[Top][All Lists]
Advanced

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

Re: Handling files with CRLF line ending


From: Chris Elvidge
Subject: Re: Handling files with CRLF line ending
Date: Fri, 9 Dec 2022 00:07:37 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 Lightning/5.4

On 08/12/2022 19:34, Ángel wrote:
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.

Regards






I didn't know that; as I said, I don't generally use Python. YThanks for the heads-up.


--
Chris Elvidge
England




reply via email to

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