ANSI colored output: How to determine how python was called?

Pearu Peterson pearu at cens.ioc.ee
Tue May 21 03:20:48 EDT 2002


On 20 May 2002, Fernando Pérez wrote:

> Pearu Peterson wrote:
> 
> > So, my question is:
> > 
> >   Are there alternative (hopefully better) ways to decide whether the
> >   output "device" of python stdout supports ANSI colored text or not?
> > 
> > Thanks,
> >         Pearu
> 
> Good luck Pearu. Let me know if you find a _robust_ solution to this problem. 
> IPython uses a lot of colors (by the way, feel free to grab the classes I've 
> made for it which automate color table management, scheme switching, etc.). 

I'll do that. Thanks. 

There are already some interesting solutions in this thread. 
Thanks to all for contribution!

> As far as I can tell, there's no sane way of determining from inside the code 
> if  a given terminal will put out garbage or if it will put out properly 
> colored text. And when you are trying to run colored text through pagers like 
> more/less, a whole new can of worms opens up! 

This can of worms is kept closed by using sys.stdout.isatty(). Try:

  python -c "import sys;print sys.stdout.isatty()"  | less

Pearu






More information about the Python-list mailing list