[Tutor] Sys.stdin Question

Steve Willoughby steve at alchemy.com
Wed Jan 14 02:55:47 CET 2009


On Tue, January 13, 2009 17:45, John Fouhy wrote:
> 2009/1/14 Damon Timm <damontimm at gmail.com>:
>> This works when I do have something coming via stdin ... but if I run
>> the script without piping something first ... it just sits there (I
>> assume, waiting for some stdin) ...

This is playing a dangerous game, though, of introducing a race condition.
 Is there nothing on the standard input RIGHT NOW because the source on
the other end of the pipe hasn't managed to generate anything yet, or
because there's nothing piped?

A better approach is either to explicitly specify whether to read from
stdin or a file, as Alan demonstrated (and the fileinput module implements
this for you, by the way), or to see if stdin is connected to a terminal
or not.  So instead of seeing if anything's showing up (and introducing
timing dependencies and uncertainty), see if it's attached to a real
terminal at all.  On Unix, os.isatty(sys.stdin) will tell you this.

steve




More information about the Tutor mailing list