Line terminators in Python?
On 29/09/17 19:54, Stefan Ram wrote:
> In some languages, printing ?'\n'?, the Unicode code point 10,
> will have the effect of printing a line terminator, which might
> mean that the output device actually receives ?\r\n?.
> The line terminator ostensibly depends on the operating
> system, but actually it depends on the output system. E.g.,
> under one operating system the console might accept another
> set of line separators than an editor. (Under Windows,
> ?wordpad? accepts ?\n?, while ?notepad? requires ?\r\n?.)
> What is the recommended way to terminate a line written with
> Python? Is it ?\n? or something else? For example, in Java,
> in some cases, one should terminate the line with the value
> of ?java.lang.System.lineSeparator()? which might or might
> not be equal to the value of ?"\n"?.
> Does it possibly depend on the entity being written to, which
> might be
> - the Python console,
> - the IDLE console,
> - the operating system console or
> - a text file?
For text I/O, Python translates \n to \r\n on Windows. For binary I/O,
it doesn't. (This means that it's important to specify text or binary
mode in your calls to open() if you want your code to be portable).
\r\n is not translated to \n on Unix. So, if you want your code to do
the right thing, use \n.