How do you read from stdin?
You could use the
import fileinputfor line in fileinput.input(): pass
fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided.
line will contain a trailing newline; to remove it use
There's a few ways to do it.
sys.stdinis a file-like object on which you can call functions
readlinesif you want to read everything or you want to read everything and split it by newline automatically. (You need to
import sysfor this to work.)
If you want to prompt the user for input, you can use
raw_inputin Python 2.X, and just
inputin Python 3.
If you actually just want to read command-line options, you can access them via the sys.argv list.
You will probably find this Wikibook article on I/O in Python to be a useful reference as well.