简介

Python的Colorama模块,可以跨多终端,显示字体不同的颜色和背景,只需要导入colorama模块即可,不用再每次都像linux一样指定颜色。

1. 安装colorama模块

pip install colorama

  

2. 常用格式常数

Fore是针对字体颜色,Back是针对字体背景颜色,Style是针对字体格式

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

注意,颜色RED,GREEN都需要大写,先指定是颜色和样式是针对字体还是字体背景,然后再添加颜色,颜色就是英文单词指定的颜色 

from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

 输出结果

# 记得要及时关闭colorma的作用范围
# 如果不关闭的话,后面所有的输出都会是你指定的颜色 print(Style.RESET_ALL)

 


3.Init关键字参数:

init()接受一些* * kwargs覆盖缺省行为,

autoreset是自动恢复到默认颜色

init(autoreset = False):

init(wrap=True):The default behaviour is to convert if on Windows and output is to a tty (terminal).

在windows系统终端输出颜色要使用init(wrap=True)

#!/usr/bin/env python
#encoding: utf-8 from colorama import init, Fore, Back, Style if __name__ == "__main__": init(autoreset=True) # 初始化,并且设置颜色设置自动恢复
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
# 如果未设置autoreset=True,需要使用如下代码重置终端颜色为初始设置
#print(Fore.RESET + Back.RESET + Style.RESET_ALL) autoreset=True
print('back to normal now')

  

输出结果

4.使用实例

import sys
import os
import random
import string
from colorama import Fore,Style,init
import platform def print_arg(arg):
"""
打印参数
:param arg:
:return:
""" for ind, val in enumerate(arg):
if ind == 0: print_color(Fore.YELLOW,r"------执行%s输入参数为--------"% val)
else:
print(val, end=",") def print_color(color, mes=""):
"""
获得系统平台
windows终端需要设置
init(wrap=True)
:param color:
:param mes:
:return:
"""
v_system = platform.system()
if v_system != 'Windows':
print(color+mes)
else:
# init(wrap=True)
print(color+mes) # 获得系统参数
v_arg = sys.argv
init(autoreset=True) # 初始化,并且设置颜色设置自动恢复
# print_color(Fore.YELLOW+platform.system())
if len(v_arg) != 4:
# print(platform.system())
print_arg(v_arg)
print_color(Fore.RED,"---参数输入错误--")
print_color(Fore.RED, "fileStrReplace.py 文件名 旧字符串 新字符串")
else:
f_name = v_arg[1].strip()
old_str = v_arg[2].strip() # 旧字符
new_str = v_arg[3].strip() # 替换的新字符
f_new_name = "%s.new" % f_name
replace_count = 0 # 字符替换次数
if not os.path.exists(f_name):
print_color(Fore.YELLOW, "%s文件不存在" % f_name)
else:
f_new = open(f_new_name, 'w')
f = open(f_name, "r",)
for line in f: # 读取大文件
if old_str in line:
new_line = line.replace(old_str, new_str) # 字符替换
replace_count += 1
else:
new_line = line f_new.write(new_line) # 内容写新文件 f.close()
f_new.close() if replace_count == 0:
print_color(Fore.YELLOW,"字符%s不存在" % (old_str))
else:
bak_f = f_name + ''.join(random.sample(string.digits, 6))
os.rename(f_name, bak_f) # 备份旧文件
os.rename(f_new_name, f_name) # 把新文件名字改成原文件的名字,就把之前的覆盖掉了
print_color(Fore.GREEN, "文件替换成功,[字符%s替换%s]共%s次,源文件备份[%s]" % (old_str,new_str, replace_count,bak_f)) # print_color(Style.RESET_ALL) # 还原默认颜色

  

Python常用模块—— Colorama模块的更多相关文章

  1. Python常用内建模块

    Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...

  2. python常用内建模块 collections,bs64,struct,hashlib,itertools,contextlib,xml

    #  2  collections 是Python内建的一个集合模块,提供了许多有用的集合类. # 2.1 namedtuple #tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: p ...

  3. Python 常用内建模块(time ,datetime)

    1,在Python中,与时间处理有关的模块就包括:time,datetime以及calendar. 2,在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(st ...

  4. python常用命令—查看模块所在位置

    环境:ipython3 交互式解释器 语法: import 模块名 模块名.__file__ 功能: 查看模块的所在位置 例:

  5. python常用函数及模块

    原文来源于博客园和CSDN 1.计算函数 abs()--取绝对值 max()--取序列最大值,包括列表.元组 min()--取序列最小值 len()--取长度 divmod(a,b)---取a//b除 ...

  6. Python 常用内建模块(os, sys,random)

    一.os 模块 1,操作系统与环境变量 import osprint(os.name) #操作系统类型,如果是posix 说明系统是linux unix 或 mac os x :如果是nt 就是win ...

  7. python常用内建模块——datetime

    datetime是python处理日期和时间的标准库. 获取当前日期和时间 >>>from datetime import datetime >>>now = da ...

  8. collections(python常用内建模块)

    文章来源:https://www.liaoxuefeng.com/wiki/897692888725344/973805065315456 collections collections是Python ...

  9. Python常用内建模块和第三方库

    目录 内建模块 1  datetime模块(处理日期和时间的标准库) datetime与timestamp转换 str与datetime转换 datetime时间加减,使用timedelta这个类 转 ...

随机推荐

  1. 《CSAPP》 可重定位目标文件格式

    可重定位目标文件 ELF文件 ELF头以一个16字节的序列开始,这个序列描述了生成该文件的系统的字的大小和字节顺序.ELF头剩下的部分包含帮助链接器语法分析和解释目标文件的信息.其中包括ELF头的大小 ...

  2. 部分用户访问Polycom视频会议时故障

    1.现象 Polycom视频会议服务器部署在防火墙下,通过Paloalto防火墙的一对一映射到公网. 部分同事使用职场网络或者4G通过公网访问时,出现超时问题. 2.分析: Polycom设备并没有做 ...

  3. 获取MessageBox按钮本地字符串(OK、Cancel、Yes、No等)

    问题仍然由定制MessageBox引发. 定制MessageBox,虽加入自定义些东西,但仍然希望,最大限度接近系统原生框.碰到的问题,就是其钮文本. 即如MessageBox.Show()之Mess ...

  4. init.d目录下的文件定义

    init.d目录下存放的一些脚本一般是linux系统设定的一些服务的启动脚本. 系统在安装时装了好多服务,这里面就有很多对应的脚本. 执行这些脚本可以用来启动,停止,重启这些服务. 1.这些链接文件前 ...

  5. Java线程池的构造以及使用

    有时候,系统需要处理非常多的执行时间很短的请求,如果每一个请求都开启一个新线程的话,系统就要不断的进行线程的创建和销毁,有时花在创建和销毁线程上的时间会比线程真正执行的时间还长.而且当线程数量太多时, ...

  6. trinitycore 魔兽服务器源码分析(二) 网络

    书接上文 继续分析Socket.h SocketMgr.h template<class T>class Socket : public std::enable_shared_from_t ...

  7. MySQL InnoDB特性:两次写(Double Write)

    http://www.ywnds.com/?p=8334 一.经典Partial page write问题? 介绍double write之前我们有必要了解partial page write(部分页 ...

  8. 2019.02.11 bzoj4818: [Sdoi2017]序列计数(矩阵快速幂优化dp)

    传送门 题意简述:问有多少长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数,且其中至少有一个数是质数,答案对201704082017040820170408取模(n≤1e9, ...

  9. php上传图片预览,放大,裁剪

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. css3简单旋转

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>&l ...