Python: file.readline adding a space at the end of the line
Question: Am using python to read a flat space-padded text file. Part of the validation
of the text file is that each line in the text file is expected to be a
specific file length including the space padding.

When I use the following code, python ends up giving me a line with an extra
space. E.g. I expect all the rows in fileX to have 143 characters. Python
though will read this as 144 characters and thus say the file is invalid. If I
do the same in VB.NET, I get the correct 143 characters.

Why is **Python's** readline function adding an extra character? (Using python
3.2)

    
    
    import io
    myfile = open("file_path", "r")
    while True:
        line = myfile.readline()
        if not line:
                break
        print(len(line))  #This prints 144 characters
    

**VB.NET** gives the correct length of 143 characters.

    
    
    Using objStreamReader As StreamReader = New StreamReader(myFilePath)
        While objStreamReader.EndOfStream = False
                line = objStreamReader.ReadLine
                len(line)   'This returns the correct length of 143.
    

Using line.strip will not be the right mechanism because I might get rid of
useful spaces. Remember the file is space-padded up to a maximum given length.


Answer: `objStreamReader.ReadLine` [chops off the terminating
newline](http://msdn.microsoft.com/en-
us/library/system.io.streamreader.readline.aspx), whereas Python's
`file.readline` [keeps
it](http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-
objects).

If your file was opened in text mode (and unless you explicitly specified
otherwise, it was), the line ending will always be either nothing (last line
only) or exactly one `\n`, and you can safely chop it off with `rstrip('\n')`.

