how to bail if stdin is empty

Jeff Epler jepler at unpythonic.net
Mon May 31 09:44:57 EDT 2004


You can test whether stdin is a "terminal", and act accordingly.

$ ./tt.py 
Standard input is a terminal.  Aborting!
$ ./tt.py < /dev/null
Standard input is a file.  Doing something.

#!/usr/bin/env python

import os
if os.isatty(0):
    print "Standard input is a terminal.  Aborting!"
else:
    print "Standard input is a file.  Doing something."
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040531/064d6472/attachment.sig>


More information about the Python-list mailing list