Testing for the presence of input from stdin.

Peter Gsellmann peter-gsellmann at eunet.at
Mon Jan 23 16:20:26 EST 2006


Will McDonald wrote:

> On 23/01/06, Diez B. Roggisch <deets at nospam.web.de> wrote:
>> > I can get the script to behave as expected when content's piped to it
>> > using sys.stdin but I'd like to know that there's data coming from
>> > stdin or fail and print the useage again. Is there a simple way to
>> > achieve this?
>>
>> There are more experienced UNIXers here, but from my POV I don't see how
>> that can happen. The reason is simply that
>>
>>  - sys.stdin alwasy exists (unless you close it yourself)
>>
>>  - in a pipe (which this essentially is) there is now way to know if
>>  there
>> is more date to come or not, except for the "broken pipe" error - but
>> that won't happen to you, as sys.stdin is not broken just because there
>> is currently no data arriving.
> 
> That's a good point. I did wonder if it'd just have to sit there
> waiting for input much like cat would. I think that's preferable, and
> simpler :), than implementing timeouts.
> 
In unix you can always use select.select() on files and pipes as sys.stdin
is. Use a timout-value of 0 and you get the 'ready-state' of the file
descriptor i.e. the presence of waiting input-data.

Peter



More information about the Python-list mailing list