Python-colored Linux Command Line terminal interface code example sharing, pythonlinux

Source: Internet
Author: User

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.

Related Article

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.