I thought this program will echo my console input line by line:
import os, sys
for line in sys.stdin:
print line
Unfortunately it waits for EOF (Ctrl + D) and then it produces output. How should I modify my program to get output line by line?
Python 2.x:
for line in iter(sys.stdin.readline, ''):
print line,
Python 3.x:
for line in iter(sys.stdin.readline, ''):
print(line, end='')
See the documentation on iter()
with two arguments, it actually has reading from a file like this as one of the examples.