Does fileinput.input() read STDIN all at once?

Tim Roberts timr at probo.com
Thu Dec 20 01:51:16 EST 2007


Adam Funk <a24061 at ducksburg.com> wrote:
>
>I'm using this sort of standard thing:
>
>   for line in fileinput.input():
>      do_stuff(line)
>
>and wondering whether it reads until it hits an EOF and then passes
>lines (one at a time) into the variable line.  This appears to be the
>behaviour when it's reading STDIN interactively (i.e. from the
>keyboard).
>
>As a test, I tried this:
>
>   for line in fileinput.input():
>      print '**', line
>
>and found that it would print nothing until I hit Ctl-D, then print
>all the lines, then wait for another Ctl-D, and so on (until I pressed
>Ctl-D twice in succession to end the loop).

Note that you are doing a lot more than just reading from a file here. This
is calling a function in a module.  Fortunately, you have the source to
look at.

As I read the fileinput.py module, this will eventually call
FileInput.next(), which eventually calls FileInput.readline(), which
eventually calls stdin.readlines(_bufsize).  The default buffer size is
8,192 bytes.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list