Python custom linux console output

Source: Internet
Author: User

How can I output color printing information to the console? How can I customize my output format? The following will tell you ......

Import OS

"""
0 All attributes off default value
1 Bold (or Bright) Bold or highlighted
4 Underline
5 Blink flashing
7. Reverse display
30 Black text
31 Red text
32 Green text
33 Yellow text
34 Blue text
35 Purple text
36 Cyan text
37 White text
40 Black background
41 Red background
42 Green background
43 Yellow background
44 Blue background
45 Purple background
46 Cyan background
47 White background
"""

Def colored (text, color = None, on_color = None, attrs = None ):
Fmt_str = '\ x1B [; % dm % s \ x1B [0m'
If color is not None:
Text = fmt_str % (color, text)

If on_color is not None:
Text = fmt_str % (on_color, text)

If attrs is not None:
For attr in attrs:
Text = fmt_str % (color, text)

Return text

Def printError (msg ):
Print colored (msg, color = 31)

Def printWarning (msg ):
Print colored (msg, color = 33)

Def printInfo (msg ):
Print colored (msg, color = 37)

If _ name _ = '_ main __':
PrintError ("this is an error message! ")
PrintWarning ("this is a warning message! ")
PrintInfo ("this ia a info message! ")

Output:

Refer:
Http://www.oschina.net/code/snippet_87675_8599
Http://pypi.python.org/pypi/termcolor/0.1.1#downloads

 

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.