What is the best way to handle a missing newline in the following case

danmcleran at yahoo.com danmcleran at yahoo.com
Fri Nov 5 11:37:13 EDT 2010


> The problem is when I get to the last line. When the program sees '\n'
> after the 9, everything works fine. However, when there isn't a '\n',
> the program doesn't process the last line.
>
> What would be the best approach to handle the case of the possible
> missing '\n' at the end of the file?

use readines to read all lines into a list and then iterate thru the
list:

f = open(r'c:\test.txt', 'rb')
print f.readlines()

>>
['1\r\n', '3\r\n', '5\r\n', '7\r\n', '3\r\n', '9']



More information about the Python-list mailing list