Is there an easy way to get the type of line ending that the current operating system uses?
The new line character in Python is \n . It is used to indicate the end of a line of text.
In Python, the new line character “\n” is used to create a new line.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With