Detecting pipe on stdout?

Alexander Schreiber als at usenet.thangorodrim.de
Wed Jul 24 10:32:17 EDT 2002


Hi!

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.

Reason: I've got a Python program[0] here which normally colorizes its
output (to the terminal) using escape sequences. This is fine and nice
while running on a terminal, but when running the program with less
attached to it the escape sequences are annoying. Ideally, I want to
detect a pipe attached to stdout and switch of the color (the flag is
already there) in this case.

Regards,
      Alex.
[0] Not written by me.
-- 
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