with open(fn, 'rt') as f:
lines = f.readlines()
This reads CR LF text file (WinXP, Py 2.6) with LF line ends. So lines contain '\n' ends. How to get lines as is:
- for CRLF file get lines with '\n\r' ends
- for LF file get lines with '\n' ends
Instead of the built-in
open()function, useio.open(). This gives you more control over how newlines are handled with thenewlineargument:Setting
newlineto the empty string, leaves universal newline support enabled but returns line endings untranslated; you can still use.readlines()to find lines terminated with any of the legal line terminators but the data returned is exactly that found in the file:Emphasis mine.
This is different from opening the file in binary mode, where
.readlines()will only split the file on\ncharacters. For a file with\rline endings or mixed line endings, this means that lines are not going to be split correctly.Demo:
Note that
io.open()also decodes file contents to unicode values.