"Damon Timm" <damont...@gmail.com> wrote

$ ls -la | myscript.py

cli_input = sys.stdin.read()

if cli_input:
 print "I piped this in:", cli_input

This works when I do have something coming via stdin ... but if I run
the script without piping something first ... it just sits there (I
assume, waiting for some stdin) ...

Yep, if you hit Ctr-D (ie. EOF) it should continue as normal.

The way other Unix style programs deal with this is to write the code
to read from a general file and if you want to use stdin provide an
argument of '-':

$ ls -la | myscript.py -

Or sometimes use a if argument to specify a file which can then
be '-' for stdin:

$ ls -la | myscript.py -f-

Either of these will enable your script to email a file if asked,
read from nowhere if no file is specified or from stdin if specifically
told to. Of course the standard utility approach is to read from
stdin as default in which case no flags are needed but you have
to provide the EOF manually.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to