Using Python to read newline characters correctly in Linux
Question: In Windows, if I'm using Python's "for line in file" syntax to read the
contents of a normal text file, the final line will not end with a newline
character, but all the lines before it will. In Linux, this behavior seems to
be different -- when I read a file line-by-line and print its contents, I
always find that the final line ends with a newline character, even if one
didn't exist in the real file! I've also tried using the file.readline()
function, with identical results.

(I'm assuming that the text file does actually not end in a newline. If it
does, Linux prints two newlines from the end of the file, instead of just the
one that really exists.)

My code is very simple. The problem is that it produces different results in
Linux than in Windows.

    
    
    with open(path, 'r') as file:
        for line in file:
            print('>', line, '<', sep='')
    
        '''
        # Alternate method:
        while True:
            line = file.readline()
            if not line:
                break
            print('>', line, '<', sep='')
        '''
    

On Linux (Ubuntu 13.10 to be exact), the final line always ends with a phantom
newline. I've tried in both Python 2 and 3. Any idea what's going on here?


Answer: What about removing the line break? Is this what you want?

    
    
    with open('/Users/sebastian/Desktop/Untitled.txt', 'r') as file:
        for line in file:
            line = line.strip()
            if line:
                print('>', line, '<', sep='')
    

prints

    
    
    >This is the first line<
    >this is the second line<
    >this is the third line<
    

PS: You will have problems with your alternate method if there is an empty
line somewhere in the middle of your text file.

### EDIT

Have a look at [PEP278](http://legacy.python.org/dev/peps/pep-0278/). The 'U'
mode might be helpful in your case

    
    
    with open('/Users/sebastian/Desktop/Untitled.txt', 'rU') as file:
        for line in file:
            line = line.strip()
            print('>', line, '<', sep='')
    

prints

    
    
    >This is the first line<
    >this is the second line<
    ><
    >this is the fourth line (blank line above)<
    ><
    

And alternatively, you can add an extra line that is platform-specific, e.g.,
via

import platform

    
    
    if platform.system()=='Windows':
        # do sth
    else:
        # do sth
    

