What's behind sys.stdin.readlines()

Viewed 2798

Question 1: I have a piece of code like this (Python2.7):

for line in sys.stdin.readlines():
    print line

When I run this code, input a string in the terminal and press Enter key, nothing happens. 'print line' doesn't work. So I imagine there is buffer for sys.stdin.readlines(), but I wonder how does it work? Can I flush it so every time a line is given, 'print line' can be executed imeediatly?

Question2: What's the difference between these two lines:

for line in sys.stdin:
for line in sys.stdin.readline():

I found their behavior is a little different. If I use ctrl+D to terminate the input, I have to press ctrl+D twice in the first case before it's really terminated. While in the second case, only one ctrl+D is enough.

1 Answers
Related