Check for new line character?

Erik Max Francis max at alcyone.com
Sun Oct 26 19:37:12 EST 2003


John Roth wrote:

> If you're using <file>.readline() to
> read the lines, then each line (except the eof signal) ***will*** end
> with a newline, and it will not have a newline internally, so there's
> no
> need for a complicated check.

Not quite true.  It's possible the file will be missing the final
newline, though Python will still treat it as a separate line:

max at oxygen:~/tmp% echo newline > test
max at oxygen:~/tmp% echo -n "no newline" >> test
max at oxygen:~/tmp% python
Python 2.3.2 (#1, Oct  3 2003, 15:44:45) 
[GCC 3.2.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = file('test')
>>> f.readline()
'newline\n'
>>> f.readline()
'no newline'
>>> f.readline()
''

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ To endure what is unendurable is true endurance.
\__/  (a Japanese proverb)




More information about the Python-list mailing list