python也可以像shell一样终端打印彩色消息.方法有很多.
1.直接打印输出
2.使用python里colorama模块.(debian7自带python-colorama模块)
下载地址wget
root@10.1.6.200:colorama-0.2.5# ./setup.py buildrunning buildrunning build_pycreating buildcreating build/libcreating build/lib/coloramacopying colorama/ansi.py -> build/lib/coloramacopying colorama/winterm.py -> build/lib/coloramacopying colorama/win32.py -> build/lib/coloramacopying colorama/__init__.py -> build/lib/coloramacopying colorama/ansitowin32.py -> build/lib/coloramacopying colorama/initialise.py -> build/lib/coloramaroot@10.1.6.200:colorama-0.2.5# ./setup.py installrunning installrunning buildrunning build_pyrunning install_libcreating /usr/lib/python2.5/site-packages/coloramacopying build/lib/colorama/ansi.py -> /usr/lib/python2.5/site-packages/coloramacopying build/lib/colorama/winterm.py -> /usr/lib/python2.5/site-packages/coloramacopying build/lib/colorama/win32.py -> /usr/lib/python2.5/site-packages/coloramacopying build/lib/colorama/__init__.py -> /usr/lib/python2.5/site-packages/coloramacopying build/lib/colorama/ansitowin32.py -> /usr/lib/python2.5/site-packages/coloramacopying build/lib/colorama/initialise.py -> /usr/lib/python2.5/site-packages/coloramabyte-compiling /usr/lib/python2.5/site-packages/colorama/ansi.py to ansi.pycbyte-compiling /usr/lib/python2.5/site-packages/colorama/winterm.py to winterm.pycbyte-compiling /usr/lib/python2.5/site-packages/colorama/win32.py to win32.pycbyte-compiling /usr/lib/python2.5/site-packages/colorama/__init__.py to __init__.pycbyte-compiling /usr/lib/python2.5/site-packages/colorama/ansitowin32.py to ansitowin32.pycbyte-compiling /usr/lib/python2.5/site-packages/colorama/initialise.py to initialise.pycrunning install_egg_infoWriting /usr/lib/python2.5/site-packages/colorama-0.2.5.egg-info
root@10.1.6.200:~# cat color.py #!/usr/bin/env python#coding:utf8#Available formatting constants are:: #Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. #Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. #Style: DIM, NORMAL, BRIGHT, RESET_ALLfrom colorama import Fore,Back,Styleprint Fore.RED + 'some red text'print Back.CYAN + 'and with a cyan background'print Style.DIM + 'and in dim text'print(Fore.RESET + Back.RESET + Style.RESET_ALL)print('back to normal now')
3.使用termcolor模块(下载地址:wget https://pypi.python.org/packages/source/t/termcolor/termcolor-1.1.0.tar.gz)
root@10.1.6.201:termcolor-1.1.0# ./setup.py buildrunning buildrunning build_pycreating buildcreating build/lib.linux-x86_64-2.7copying termcolor.py -> build/lib.linux-x86_64-2.7root@10.1.6.201:termcolor-1.1.0# ./setup.py installrunning installrunning buildrunning build_pyrunning install_libcopying build/lib.linux-x86_64-2.7/termcolor.py -> /usr/local/lib/python2.7/dist-packagesbyte-compiling /usr/local/lib/python2.7/dist-packages/termcolor.py to termcolor.pycrunning install_egg_infoWriting /usr/local/lib/python2.7/dist-packages/termcolor-1.1.0.egg-info
参考: