Testing for the presence of input from stdin.
Fredrik Lundh
fredrik at pythonware.com
Tue Jan 24 04:44:30 EST 2006
Will McDonald wrote:
> > 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.
the usual way to implement this is to treat the filename "-" as stdin.
if filename == "-":
f = sys.stdin
else:
f = open(filename)
... read from f ...
if f is not sys.stdin:
f.close()
</F>
More information about the Python-list
mailing list