Python gets proper line termination

Is there an easy way to get the line ending type used by the current operating system?

+46
python line-endings
Jan 18 '09 at 6:01
source share
3 answers

If you are working with a file that you opened in text mode, then you are correct that line breaks all appear as " \n ". Otherwise, you are looking for os.linesep .

From http://docs.python.org/library/os.html :

wasps linesep

A string used to split (or rather complete) strings on the current platform. It can be one character, for example, '\ n' for POSIX, or several characters, for example, '\ r \ n' for Windows. Do not use os.linesep as a line terminator when writing files opened in text mode (by default); use one "\ n" instead, on all platforms.

+75
Jan 18 '09 at 8:21
source share

Oh, I get it. Apparently, PEP-278 states the following:

Any line ending with an input file will look like "\ n" in Python, so a small, different code value must change to handle universal newline characters.

+14
Jan 18 '09 at 6:07
source share

If set to test resp. it is binary correct when opening files and using universal newlines, you no longer have to worry about different news.

But if you need to use os.linesep

+3
Jan 18 '09 at 8:21
source share



All Articles