Brief introduction
Python's Colorama module, which can be used to display different colors and backgrounds across multiple terminals, requires only the Colorama module to be imported, without having to assign colors like Linux every time.
1. Installing the Colorama module
Pip Install Colorama
2. Common Format Constants
Fore is for font color, back is for font background color, style is for font format
Fore:black, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, White, RESET. Back:black, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, White, RESET. Style:dim, NORMAL, BRIGHT, Reset_all
Note that the color red,green all require uppercase, first specify whether the color and style are for the font or font background, and then add the color, color is the English word specified color
From Colorama import Fore, back, Styleprint (fore.red + ' some RED text ') print (Back.green + ' and with a GREEN background ') PR Int (Style.dim + ' and in DIM text ') print (style.reset_all) print (' Back to normal now ')
Output results
# Remember to close the Colorma in time # If you don't close, all the output will be the color you specified (Style.reset_all)
3.Init keyword Parameters:
Init () accepts some * * Kwargs overrides the default behavior,
Autoreset is automatically restored to the default color
init (Autoreset = False):
Init (wrap=true): The default behaviour is to convert if on Windows and output are to a TTY (terminal).
Output colors in Windows system terminal to use init (wrap=true)
#!/usr/bin/env python #encoding: utf-8from colorama import init, Fore, back, styleif __name__ = = "__main__": init (auto reset=true) # Initialize and set the color settings to automatically resume print (fore.red + ' some RED text ') print (Back.green + ' and with a GREEN Background ') print (Style.dim + ' and in DIM text ') # If Autoreset=true is not set, you need to reset the terminal color to the initial setting using the following code #print ( Fore.reset + back.reset + style.reset_all) autoreset=true print (' Back to normal now ')
Output results
4. Usage examples
Import sysimport osimport randomimport stringfrom colorama import fore,style,initimport platformdef print_arg (ARG): "" " Printing parameters:p Aram ARG:: Return: "" "for Ind, Val in enumerate (ARG): if ind = = 0:print_color (F Ore. Yellow,r "------Execution%s input parameter is--------"% val) Else:print (Val, end= ",") def print_color (color, mes= ""): "" " Get system platform Windows terminal needs to set init (wrap=true):p Aram color::p Aram mes:: Return: "" "V_system = Platform . System () if v_system! = ' Windows ': print (color+mes) Else: # init (wrap=true) print (color+mes) # Get system Parameters V_arg = Sys.argvinit (autoreset=true) # Initialize and set the color settings AutoRecover # Print_color (Fore.yellow+platform.system ()) If Len (v_ ARG)! = 4: # print (Platform.system ()) Print_arg (V_arg) Print_color (fore.red, "---parameter input error--") Print_color (fore.re D, "filestrreplace.py filename old string new string") Else:f_name = V_arg[1].strip () Old_str = V_arg[2].strip () # old character new_str = V_arg[3].strip () # New characters to replace F_new_name = "%s.new"% f_name replace_count = 0 # character substitution if not os.path.exists (f_name): Print_color (Fore . YELLOW, "%s file does not exist"% f_name) else:f_new = open (F_new_name, ' w ') F = open (F_name, "R",) for line in F: # Read large file if old_str in line:new_line = Line.replace (Old_str, new_str) # character substitution Replace_count + = 1 Else:new_line = line F_new.write (new_line) # Content write new file F.C Lose () F_new.close () if Replace_count = = 0:print_color (Fore.yellow, "character%s does not exist"% (OLD_STR)) Else:bak_f = F_name + ". Join (Random.sample (string.digits, 6)) Os.rename (F_name, Bak_f) # Back up old files Os.rename (F_new_name, f_name) # Change the name of the new file to the name of the original file, then overwrite the previous print_color (Fore.green, "file replacement succeeded, [character%s replace%s] Total %s times, source file backup [%s] "% (Old_str,new_str, Replace_count,bak_f)) # Print_color (style.reset_all) # Restore default color
Python Common Module--colorama module