33

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.

1

6 Answers 6

56

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)
Sign up to request clarification or add additional context in comments.

7 Comments

+1 especially for sys.stdout.isatty()
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
unbuffer can do that, so you're not stuck if there's no override.
@Ignacio, cool I wonder why debian doesn't have an unbuffer package :(
found it - debian hides it in expect-dev under the name expect_unbuffer
|
12

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 ...

Comments

5

All the major color codes are given at https://www.siafoo.net/snippet/88

3 Comments

This website's security certificate has expired. Can anyone verify that this is a safe website?
@BlackVegetable, yeah, it looks the same as before. Also, you can view it HTTP if you prefer. I'll contact them to let them know about the cert.
even if you do trust it, @BlackVegetable, you get a proxy error
4

curses will allow you to use colors properly for the type of terminal that is being used.

Comments

2

have a look at http://www.pixelbeat.org/talks/python/ls.py

Comments

-4

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?')

2 Comments

This sets the color globally, for the entire terminal, not for some characters on it.
Doesn't work at all in the spyder console or spyder IPython console.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.