stdio EOF ?

Paul Rubin phr-n2002b at
Tue Aug 13 13:31:51 CEST 2002

Duncan Booth <duncan at> writes:
> If you don't like having to strip all the newlines from each line, then an 
> alternative is to write a generator to do it for you:

> from __future__ import generators
> import sys
> def filelines(f):
>     while 1:
>         line = f.readline()
>         if not line: break
>         yield line[:-1]
> for line in filelines(sys.stdin):
>     print "**",`line`,"**"

Oops!  This chops the last character if the last line of the file
doesn't end with newline.

More information about the Python-list mailing list