Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get proper line ending

Is there an easy way to get the type of line ending that the current operating system uses?

like image 514
Evan Fosmark Avatar asked Jan 18 '09 06:01

Evan Fosmark


People also ask

How do you get to the end of a line in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text.

How do I make a line break in Python?

In Python, the new line character “\n” is used to create a new line.

What is a line terminator Python?

The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as '\n' for POSIX, or multiple characters, for example, '\r\n' for Windows.

How do you break a long string in Python?

You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python.


1 Answers

If you are operating on a file that you opened in text mode, then you are correct that line breaks all show up as '\n'. Otherwise, you are looking for os.linesep .

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

os.linesep

The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as '\n' for POSIX, or multiple characters, for example, '\r\n' for Windows. Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.

like image 135
dF. Avatar answered Sep 21 '22 22:09

dF.