Detecting pipe on stdout? -- col

DIG dig.list at telkel.net
Wed Jul 24 14:01:07 EDT 2002


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


HTH,

-- 
DIG (Dmitri I GOULIAEV)




More information about the Python-list mailing list