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



On Dec 18, 5:55 am, Adam Funk <a24...@xxxxxxxxxxxxx> 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).


There is probably a 1024 byte buffer. Try filling it up and see if you
get something before you hit CTRL-D.

Otherwise, the writers have to flush the buffer if they want a line to
be sent before the buffer fills up. C++ endl would do this, I believe,
in addition to printing '\n'.

Note that terminals (what you get if you are typing from the command
line) are terminals and behave quite differently than open files.

Is it possible to configure this to pass each line of input into line
as it comes?


It's up to the writer to flush the buffers. There's not much you can
do, so just learn to live with it.

It sounds like you want to write some kind of interactive program for
the terminal. Do yourself a favor and use curses or go with a full-
blown GUI.
.



Relevant Pages

  • Re: process a BIG string
    ... I won't hit the memory limit because i only read about 10 to 64Kb at a ... in buffers of about 8192bytes and process each buffer, ... the problem is that with large filesi get a script error(Fatal ... You might also hit the roof of your memory usage if you handle and process ...
    (comp.lang.php)
  • Re: [Bug #14015] pty regressed again, breaking expect and gccs testsuite
    ... 'space' logic) just means that we now hit a case that we didn't use to ... I think I have a clue. ... room for two characters. ... the receive buffer (including all the data that was already buffered up, ...
    (Linux-Kernel)
  • init file hitting keys for me
    ... How do I tell .emacs to make every new buffer I open behave like I hit ... And I want emacs to do that for me. ...
    (comp.emacs)
  • Re: Inverters and Moderators with their head up their own arses
    ... I *know* an inverter could kill given a *very* specific set of circumstances - I have even given the circumstances several times. ... And for the last time - you'd have to touch both terminals *exactly* at the same time (and not a millisecond out) *and* be stood on insulation. ... Just like you have to hit Ctrl+Alt+Delete at the 'exactly' the same time!!!! ...
    (uk.legal)
  • Re: select item in kill ring / buffer list by number
    ... I would find it useful to yank the nth item of the kill-ring, ... Hit C-y: ... show the nth buffer in the buffer list (as shown by list-buffers or in ... Searching for buffer cycling leads ...
    (comp.emacs.xemacs)