python: print out coloured strings

https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-terminal-in-python

Define a color class

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

The way to use it:

print bcolors.WARNING + "Warning: No active frommets remain. Continue?" 
      + bcolors.ENDC

including the start and end parts. The end part is important, otherwise, all the following printing will have the same color as used in current line.

This can work on OS X, linux and windows, already tested on centOS 7, it works well.

你可能感兴趣的:(python: print out coloured strings)