0

I have code in python that looks like this:

for line in sys.stdin:
    print line + "END"

I read that this iterates over stdin line by line, but honestly the command line output makes me think it's reading all of stdin and then iterating line by line; it's printing data, stopping, then printing more. I tested this by making a counter like so:

for line in sys.stdin:
    print i
    i+=1
    sys.stdout.flush()

It would stop printing at 96, then at 218, etc., each for about half a second, before printing a whole bunch more numbers at the same time.

Is there a way of iterating over lines that executes the things in the loop before reading the next line?

user3475234
  • 1,503
  • 3
  • 22
  • 40

0 Answers0