Python color Linux command-line terminal Interface code example sharing

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.