Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to bail if stdin is empty

Reply
Thread Tools

how to bail if stdin is empty

 
 
Jon Schull
Guest
Posts: n/a
 
      05-31-2004
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!
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      05-31-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."

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

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

 
Reply With Quote
 
 
 
 
Antoon Pardon
Guest
Posts: n/a
 
      06-01-2004
Op 2004-05-31, Jon Schull schreef <(E-Mail Removed)>:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Did Pentax bail out of commie Vietnam? RichA Digital Photography 12 11-11-2010 05:38 PM
Who will bail on DSLRs first, Sony or Olympus? RichA Digital Photography 9 06-30-2010 04:34 AM
French government to bail out their banks Aardvark Computer Support 7 10-22-2008 02:25 AM
Steve-O: Volume III: Out On Bail DVD Review @ GENRE ONLINE.NET! Writer R5 DVD Video 0 12-08-2003 12:46 AM



Advertisments