You can use the fileinput input module : this module basically implements a helper class and a function to write a loop over standard input or a list of files.
Syntax-
import fileinput
for line in fileinput.input():
process(line)
fileinput loops through all in the input which has been specified as file names in the command-line arguments and if arguments are not provided, it loops through all the standard input.