First look at the effect:
In Linux terminal, ANSI escape sequence to control color
Basic rule: Precede with \033[, end with \033[0m reset to original color
You can enter the following sentence in the terminal, you can see the output of the green hello.
>>echo-e ' \033[0;32mhello\033[0m '
where 0;32m controls the color.
The simplest, as long as the 0;32m in the 2 change to 0-7, it corresponds to different colors.
Using this, in Python, you can do this.
#coding =utf-8 FMT = ' \033[0;3{}m{}\033[0m '. Format class color: Black = 0# Dark Red = # red green = * Green YELLOW = 3# Brown Blue = 4# Blue PURPLE = AA Violet CYAN = 6# Cyan Gray = 7# Gray print fmt (color. BLACK, ' Kzc ') print fmt (color. RED , ' kzc ') print fmt (color. GREEN, ' Kzc ') print fmt (color. YELLOW, ' Kzc ') print fmt (color. BLUE , ' kzc ') print fmt (color. PURPLE, ' Kzc ') print fmt (color. CYAN , ' kzc ') print fmt (color. GRAY
Gracefully execute the program under Ps:linux
Under Linux, we execute a python program that is python/path/to/xxx.py.
If this program is used frequently, it will feel a bit troublesome.
You can chmod +x/path/to/xxx.py, that is, to add executable permissions to this file, you can not hit Python in front, directly/path/to/xxx.py run.
However, for people with code cleanliness, this is not elegant enough, followed by a. py suffix.
Remove the. py suffix as long as you add #!/usr/bin/python to the first line of the file.
Then the direct/path/to/xxx can be executed.