Python3学习之路~5.14 模块补充:logging & getpass 模块
logging模块
#loggingtest/conf/test.conf
####日志文件位置####
[log]
logfile = ../log/test.log
# loggingtest/core/test.py import os,configparser,logging ####读取配置文件#### base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) config_file = os.path.join(base_dir, 'conf/test.conf') cf = configparser.ConfigParser() cf.read(config_file, encoding='utf-8') ####设定日志目录#### if os.path.exists(cf.get('log', 'logfile')): logfile = cf.get('log', 'logfile') else: logfile = os.path.join(base_dir, 'log/client.log') ####设置日志格式#### logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename=logfile, filemode='a+') logging.debug("msg1") logging.info("msg2") logging.warning("msg3") logging.error("msg4") logging.critical("msg5") # 输出中没打印msg1,这是因为设置了级别为info,因此只打印大于等于info级别的日志 #日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG
# loggingtest/log/test.log 2018-10-29 16:04:41 INFO msg2 2018-10-29 16:04:41 WARNING msg3 2018-10-29 16:04:41 ERROR msg4 2018-10-29 16:04:41 CRITICAL msg5
更多内容请看这里
getpass模块
import getpass username = input("请输入用户名>>>:").strip() password = getpass.getpass("请输入密码>>>:").strip() ####使输入密码不显示,此模块在pycharm中无法使用 print(username,password)