Detecting pipe on stdout?

Alexander Schreiber als at usenet.thangorodrim.de
Wed Jul 24 14:39:43 EDT 2002


Alex Martelli <aleax at aleax.it> wrote:
>Alexander Schreiber wrote:
>
>> Is there any way for a Python program to detect wether its stdout has
>> been redirected to a pipe/file (like "./programm | less")? I've didn't
>> find anything about this in the FAQ and failed to come up with the right
>> search terms for google.
>
>You probably want os.isatty(sys.stdout.fileno()).  Not quite what
>you ask for (it's false whether the redirection is to a pipe OR a
>file), but probably what you want.

Yes, thats what I want - I was thinking of less glued to stdout, but the
same applies to redirecting stdout somewhere else (like a file).
I'm now using sys.stdout.isatty() as suggested by another poster and it
does the job. Thanks.

Regards,
      Alex.
-- 
Those who desire to give up Freedom in order to gain Security, will not
have, nor do they deserve, either one.              -- Benjamin Franklin



More information about the Python-list mailing list