Detecting pipe on stdout? -- col

Ivica StRanGy at cna2.kav.vest.hr
Fri Jul 26 03:59:14 EDT 2002


On Wed, 24 Jul 2002 13:01:07 -0500, DIG <dig.list at telkel.net> wrote:
> Hi, Alexander Schreiber !
> 
>  On Wed, Jul 24, 2002 at 02:32:17PM +0000, 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.
>> 
>> 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.
> 
> It is not EXACT answer to your question, but I hope this could help you to solve your problem: you could use ``col'' command (if you are using Linux).
> 
> From ``man col'':
>      [...]
>      col - filter reverse line feeds from input
>      [...]
>      All unrecognized control characters and escape sequences are discarded.
>      [...]
> 
> So, you could use it like this:
> 
>   $ program | col | less

Why not do a program | less -R
R stand for
R                    Repaint screen, discarding buffered input.



More information about the Python-list mailing list