[Tutor] how to check for stdin?

christian folini christian.folini@unifr.ch
Tue, 6 Feb 2001 11:00:55 +0100


On 2001.02.05 16:45:30 +0100 Michael P. Reilly wrote:
> I doubt that you could check on windoze, but in a UNIX environment
> there is knowledge of whether a open file is attached to a terminal or
> not.  A method called "isatty()" returns true if associated with a
> terminal.

thank you for that hint. Happy enough my script need not run
under windows... And it works. cool.

however, there was a typo on the second line, your prg should read:

$ cat eggs.py
import sys
if not sys.stdin.isatty(): # redirected from file or pipe
  stdin_data = sys.stdin.read()
else:
  stdin_data = 'not read from stdin'
print `stdin_data`

cheers, christian