[Tutor] Sys.stdin Question

Damon Timm damontimm at gmail.com
Wed Jan 14 01:34:09 CET 2009


Hi - am writing a script to send myself email messages from the
command line ... I want to have the option be able to input the
message body via a pipe so I can easily shoot emails to myself (like
from: ls, cat, df, du, mount, etc) ... what i want to be able to do
is:

$ ls -la | myscript.py

and in the script use something like this (just an example to keep it short):

cli_input = sys.stdin.read()

if cli_input:
  print "I piped this in:", cli_input
else:
  print "nothing got piped in, moving on."

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) ...

How do I tell it: if there is no stdin, just move on?

Thanks,
Damon


More information about the Tutor mailing list