Testing for the presence of input from stdin.

Roland Heiber newstonne at web.de
Tue Jan 24 03:57:19 EST 2006

Will McDonald wrote:
> Hi all.
> I'm writing a little script that operates on either stdin or a file
> specified on the command line when run. I'm trying to handle the
> situation where the script's run without any input gracefully but
> can't think how to test for stdin.


maybe http://docs.python.org/lib/module-fileinput.html is useful for you:

"This iterates over the lines of all files listed in sys.argv[1:], 
defaulting to sys.stdin if the list is empty. If a filename is '-', it 
is also replaced by sys.stdin. To specify an alternative list of 
filenames, pass it as the first argument to input(). A single file name 
is also allowed."

HtH, Roland

More information about the Python-list mailing list