Python color Linux command line terminal interface Code instance sharing _python

Source: Internet
Author: User

First look at the effect:

In the Linux terminal, the ANSI escape sequence controls the color
Basic rules: Front plus \033[, end with \033[0m reset to original color
You can enter the following sentence in the terminal, you can see the output green hello.

>>echo-e ' \033[0;32mhello\033[0m '

where 0;32m controls color.
The simplest, as long as the 0;32m 2 to 0-7, it corresponds to different colors.

Take advantage of 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 = 2 #绿 
  yellow = 3# Brown 
  Blue  = 4# Blue 
  PURPLE = 5# Violet 
  cyan  = 6# Green 
  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  , ' Kzc ') 

Ps:linux gracefully executes the program
under Linux, we execute a python program that is python/path/to/xxx.py.
This is a bit of a hassle if the program is used frequently. The
can chmod +x/path/to/xxx.py, that is, add executable permissions to the file, so you can run it directly/path/to/xxx.py without knocking python on the front.
However, this is not elegant enough for someone with code cleanliness, with a. py suffix behind it.
Remove the. py suffix, as long as you add #!/usr/bin/python to the first line of the file. The
is then directly/path/to/xxx to execute.

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.