Detecting pipe on stdout?

Denis S. Otkidach ods at fep.ru
Wed Jul 24 10:40:44 EDT 2002


On 24 Jul 2002, Alexander Schreiber wrote:

AS> Is there any way for a Python program to detect wether its
AS> stdout has
AS> been redirected to a pipe/file (like "./programm | less")?
AS> I've didn't
AS> find anything about this in the FAQ and failed to come up
AS> with the right
AS> search terms for google.
AS>
AS> Reason: I've got a Python program[0] here which normally
AS> colorizes its
AS> output (to the terminal) using escape sequences. This is
AS> fine and nice
AS> while running on a terminal, but when running the program
AS> with less
AS> attached to it the escape sequences are annoying. Ideally, I
AS> want to
AS> detect a pipe attached to stdout and switch of the color
AS> (the flag is
AS> already there) in this case.

sys.stdout.isatty()

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]
http://diveinto.python.ru/ [ru]





More information about the Python-list mailing list