I was wondering if anyone knows how to set the color of the text that shows up in the shell. I noticed the 'ls' uses a couple different colors when printing out information to the screen (on my Linux box), was wondering if I could take advantage of that in Python.
6 Answers
Use Curses or ANSI escape sequences. Before you start spouting escape sequences, you should check that stdout is a tty. You can do this with sys.stdout.isatty()
. Here's a function pulled from a project of mine that prints output in red or green, depending on the status, using ANSI escape sequences:
def hilite(string, status, bold):
attr = []
if status:
# green
attr.append('32')
else:
# red
attr.append('31')
if bold:
attr.append('1')
return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)

- 205,541
- 37
- 345
- 415
-
15
-
It's also nice to have an override for the case that the output is not a tty, but you still want the colour - say you are just filtering lines with sed or grep – John La Rooy Feb 24 '10 at 23:02
-
`unbuffer` can do that, so you're not stuck if there's no override. – Ignacio Vazquez-Abrams Feb 24 '10 at 23:18
-
@Ignacio, cool I wonder why debian doesn't have an unbuffer package :( – John La Rooy Feb 24 '10 at 23:26
-
-
2found it - debian hides it in `expect-dev` under the name `expect_unbuffer` – John La Rooy Feb 24 '10 at 23:32
-
Is that supposed to work in a Windows 7 console? Because when I call `print( hilite( "toto", True, True ) )` it prints `←[32;1mtoto←[0m` with no color....(note that `sys.stdout.isatty()` returns me `True`) – jpo38 Jun 23 '16 at 15:00
I just described very popular library clint. Which has more features apart of coloring the output on terminal.
By the way it support MAC, Linux and Windows terminals.
Here is the example of using it:
Installing (in Ubuntu)
pip install clint
To add color to some string
colored.red('red string')
Example: Using for color output (django command style)
from django.core.management.base import BaseCommand
from clint.textui import colored
class Command(BaseCommand):
args = ''
help = 'Starting my own django long process. Use ' + colored.red('<Ctrl>+c') + ' to break.'
def handle(self, *args, **options):
self.stdout.write('Starting the process (Use ' + colored.red('<Ctrl>+c') + ' to break)..')
# ... Rest of my command code ...

- 9,615
- 4
- 51
- 65
All the major color codes are given at https://www.siafoo.net/snippet/88

- 278,309
- 50
- 514
- 539
-
3This website's security certificate has expired. Can anyone verify that this is a safe website? – BlackVegetable Feb 16 '12 at 23:43
-
1@BlackVegetable, yeah, it looks the same as before. Also, you can view it [HTTP](http://www.siafoo.net/snippet/88) if you prefer. I'll contact them to let them know about the cert. – Matthew Flaschen Feb 17 '12 at 00:42
-
curses
will allow you to use colors properly for the type of terminal that is being used.

- 776,304
- 153
- 1,341
- 1,358
This is so simple to do on a PC: Windows OS: Send the os a command to change the text: import os
os.system('color a') #green text
print 'I like green'
raw_input('do you?')

- 19
- 1
-
6This sets the color globally, for the entire terminal, not for some characters on it. – Helgi Jul 11 '12 at 20:03
-