Input from a file as a command line argument

François Pinard pinard at iro.umontreal.ca
Sun Apr 6 00:08:09 CEST 2003


[Thomas Guettler]

> import sys
> for line in sys.stdin.xreadlines():
>     print line

In Python 2.2, this should be better written:

   import sys
   for line in sys.stdin:
       print line

However, I noticed this morning that whenever sys.stdin is connected to an
interactive terminal, input lines are buffered instead of passed one by one
to the application after each `Enter'.  So, for this application, I had to
revert to the older paradigm:

   import sys
   line = sys.stdin.readline():
   while line:
       print line
       line = sys.stdin.readline()

I wonder if this might be considered as a Python bug, or not.  What is the
general opinion? :-)

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list