Python-colored Linux Command Line terminal interface code example sharing, pythonlinux
First look at the effect:
In linux terminals, ANSI escape sequences are used to control colors.
Basic rule: Add \ 033 [to the front and end with \ 033 [0 m to the original color
Enter the following sentence in the terminal to see the green hello output.
>>echo -e '\033[0;32mhello\033[0m'
0; 32 m control color.
The simplest way is to change 2 of 0 and 32 m to 0-7, which corresponds to different colors.
This can be used in python.
# Coding = UTF-8 fmt = '\ 033 [0; 3 {} m {} \ 033 [0m '. format class color: BLACK = 0 # black red = 1 # red green = 2 # green yellow = 3 # Brown BLUE = 4 # blue purple = 5 # purple cyan = 6 # blue gray = 7 # GRAY print fmt (color. BLACK, 'yzc') print fmt (color. RED, 'yzc') print fmt (color. GREEN, 'yzc') print fmt (color. YELLOW, 'yzc') print fmt (color. BLUE, 'yzc') print fmt (color. PURPLE, 'yzc') print fmt (color. CYAN, 'yzc') print fmt (color. GRAY, 'yzc ')
PS: elegant program execution in Linux
In linux, a python program is python/path/to/xxx. py.
If this program is often used, it will be a little troublesome.
Chmod + x/path/to/xxx. py, which adds the executable permission to the file, so you don't have to knock on python before, directly/path/to/xxx. py is running.
However, for those who have code cleanliness, This is not elegant enough, followed by A. py suffix.
Remove the. py suffix as long as # is added to the first line of the file #! /Usr/bin/python.
Then you can directly execute/path/to/xxx.