python: print out coloured strin

2019-06-24  本文已影响0人  Andy512

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.

上一篇 下一篇

猜你喜欢

热点阅读