Iteration on file reading

Andrew Dalke adalke at mindspring.com
Sat Oct 4 02:56:01 EDT 2003


Paul McGuire:
> def lineReader( strm ):
>   while 1:
>     yield strm.readline().rstrip("\n")
>
> for f in lineReader( stdin ):
>   print ">>> " + f

You can simplify that with the iter builtin.

for f in iter(stdin.readline, ""):
  print ">>> " + f

(Hmm... maybe I should test it?  Naaaaahhh.)

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list