473,499 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6820
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6474
by: barr | last post by:
Hi Can any one help. I am trying to write a python scipt that takes input as args and/or as piped input ( possibly the output of another program). I want to read stdin ( the piped in stuuff...
7
15954
by: | last post by:
Can I append a string to stdin? How? How can I read from stdin a whole line? because scanf("%s", string) reads only a word even if I use "" thanks
2
10188
by: gc | last post by:
I was having trouble getting fgets to read a string from stdin, it was reading a '\n' already in the buffer. Someone told me to rewind stdin before calling fgets. It works, but is it permissible?
8
3886
by: orium69 | last post by:
Hi! how could i know if it was written something in stdin since the last time i read it? Tks...
7
3452
by: Will McDonald | last post by:
Hi all. I'm writing a little script that operates on either stdin or a file specified on the command line when run. I'm trying to handle the situation where the script's run without any input...
2
4469
by: Dmitry Anikin | last post by:
I want to read stdin in chunks of fixed size until EOF I want to be able (also) to supply data interactively in console window and then to hit Ctrl+Z when finished So what I do is: while True:...
1
11098
by: ywzhan | last post by:
Hi, I am new here. When I use sys.stdin.readline() to get input string from user, how can I set a timeout value for this operation? thank you.
8
3791
by: Christoph Haas | last post by:
Hi... I encountered a problem that - according to my google search - other people have found, too. Example code: import sys for line in sys.stdin: print line Running this code in Python...
0
1960
by: Oliver Bleckmann | last post by:
hey guys, i have a little problem. my cgi routines are working so far, but i need to redirect to the cgi-programm and what the "posted" data to be cleared if redirected. i don't know how the post...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.