In python, how to check the end of standard input streams (sys.stdin) and do something special on that In python, how to check the end of standard input streams (sys.stdin) and do something special on that python python

In python, how to check the end of standard input streams (sys.stdin) and do something special on that


for line in sys.stdin:    do_whatever()# End of stream!do_whatever_else()

It's that simple.


Use try/except. Input

When EOF is read, EOFError is raised.

while True:    try:        s=input("> ")    except EOFError:        print("EOF")        break