471,052 Members | 1,215 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,052 software developers and data experts.

how to bail if stdin is empty

I want to write a filter that will take input from files on the
command line and/or from stdin.

But if nothing has been piped to stdin, I don't want my program to
hang--I want it to do something else.

This is standard behavior for pipeable-programs I believe, but I can't
figure out how to do it...neither by reading from stdin nor by using
the fileinput module.

Thanks in advance!
Jul 18 '05 #1
2 6490
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."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAuzbZJd01MZaTXX0RAh/FAJ9gyjEQG3T+JpRXYTYd1C+/DsFuVwCeKAH+
JsomiFe8DWJ2YtXMKRc6Y9E=
=8zxf
-----END PGP SIGNATURE-----

Jul 18 '05 #2
Op 2004-05-31, Jon Schull schreef <sc****@digitalgoods.com>:
I want to write a filter that will take input from files on the
command line and/or from stdin.

But if nothing has been piped to stdin, I don't want my program to
hang--I want it to do something else.

This is standard behavior for pipeable-programs I believe,


No it is not. Standard behaviour for filters (on unix that is)
is to use standard input if no file arguments are given.

If you start such a program without arguments and not connected
to another programs with a pipe, such a program will hang and
wait for user input from the keyboard.

--
Antoon Pardon
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by barr | last post: by
7 posts views Thread by | last post: by
2 posts views Thread by gc | last post: by
8 posts views Thread by orium69 | last post: by
7 posts views Thread by Will McDonald | last post: by
2 posts views Thread by Dmitry Anikin | last post: by
8 posts views Thread by Christoph Haas | last post: by
reply views Thread by Oliver Bleckmann | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.