day16 learning Summary

A, os module

Function: to interact with the operating system, control files and folders.

method Detailed
File operation
os.isfile (file path) To determine whether the file
os.remove (file path) Delete Files
os.rename(r'', r'') Rename the file
Folder operations
os.path.isdir (folder path) Determine whether the folder
os.mkdir (folder path) Create a folder
os.rmdir (folder path) Delete the folder
os.listdir (folder path) List Folder All files
Supplementary
os.getcwd (folder path) Where the files in the current folder
os.path.abspath(__file__) The current path of the file where the specific
os.path.dirname(__file__) The files in the folder
os.path.join (folder path, file) Splicing file path
os.path.exists (file path) Determine whether there is a path (file or folder apply)

Two, sys module

Function: interaction with the Python interpreter.

method Detailed
sys.argv Run the file command line to receive extra parameters
sys.modules.keys() Returns a list of all currently has to import module
To understanding
sys.version Version Get Python interpreter
sys.hexversion Gets the version of Python interpreter value, hexadecimal formats such as: 0x020403F0
sys.copyright Record python copyright-related stuff
sys.api_version API versions of C interpreter

Three, json module

Serialization: the memory objects (variables) arranged into an object specific rules stored or transmitted sequence of the process is called

Function: json serialization can achieve the purpose of cross-platform data transmission.

Python and json conversion relationship is as follows:

Json type Python type
{} dict
[] list
"string" str
520.13 int or float
true/false True/False
null None

Four, pickle module

Function: pickle serialization only for python, stores all data types in python, including an object.

Five, logging module (log module)

1. Log Level

  1. info normal

    logging.info('info') # 10

  2. the debug

    logging.debug('debug') # 20

  3. warning warning

    logging.warning('wraning') # 30

  4. error error

    logging.error('error') # 40

  5. critical danger

    logging.critical('critical') # 50

2. Add settings

The basic configuration of the log

import logging

# 日志的基本配置

logging.basicConfig(filename='access.log',
                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=10)

logging.debug('调试信息')  # 10
logging.info('正常信息')  # 20
logging.warning('警告信息')  # 30
logging.error('报错信息')  # 40
logging.critical('严重错误信息')  # 50

3. The customized settings (low distribution)

  1. Configuration logger candidate: generating a log, and then filtered to Filter, and then to output different Handler
logger = logging.getLogger(__file__)
  1. Configure Filter objects: unusual, slightly
  2. Object Handler Configuration: receiving logger logs coming, then the control output
h1 = logging.FileHandler('t1.log')  # 打印到文件
h2 = logging.FileHandler('t2.log')  # 打印到文件
sm = logging.StreamHandler()  # 打印到终端
  1. Formatter objects: log format
formmater1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                               datefmt='%Y-%m-%d %H:%M:%S %p',)

formmater2 = logging.Formatter('%(asctime)s :  %(message)s',
                               datefmt='%Y-%m-%d %H:%M:%S %p',)

formmater3 = logging.Formatter('%(name)s %(message)s',)
  1. Binding format Handler object
h1.setFormatter(formmater1)
h2.setFormatter(formmater2)
sm.setFormatter(formmater3)
  1. Handler will add to the logger and set the log level
logger.addHandler(h1)
logger.addHandler(h2)
logger.addHandler(sm)
  1. Set the log level can be set in two levels logger and handler
# logger是第一级过滤,然后才能到handler
logger.setLevel(30)
h1.setLevel(10)
h2.setLevel(10)
sm.setLevel(10)
  1. test
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')

4. Custom settings (OW)

4.1 configuration log file

import os
import logging.config

# 定义三种日志输出格式 开始
standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
                  '[%(levelname)s][%(message)s]'  # 其中name为getLogger()指定的名字;lineno为调用日志输出函数的语句所在的代码行
simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
# 定义日志输出格式 结束

logfile_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # log文件的目录,需要自定义文件路径 # atm
logfile_dir = os.path.join(logfile_dir, 'log')  # C:\Users\oldboy\Desktop\atm\log

logfile_name = 'log.log'  # log文件名,需要自定义路径名

# 如果不存在定义的日志目录就创建一个
if not os.path.isdir(logfile_dir):  # C:\Users\oldboy\Desktop\atm\log
    os.mkdir(logfile_dir)

# log文件的全路径
logfile_path = os.path.join(logfile_dir, logfile_name)  # C:\Users\oldboy\Desktop\atm\log\log.log
# 定义日志路径 结束

# log配置字典
LOGGING_DIC = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': standard_format
        },
        'simple': {
            'format': simple_format
        },
    },
    'filters': {},  # filter可以不定义
    'handlers': {
        # 打印到终端的日志
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',  # 打印到屏幕
            'formatter': 'simple'
        },
        # 打印到文件的日志,收集info及以上的日志
        'default': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件
            'formatter': 'standard',
            'filename': logfile_path,  # 日志文件
            'maxBytes': 1024 * 1024 * 5,  # 日志大小 5M  (*****)
            'backupCount': 5,
            'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
        },
    },
    'loggers': {
        # logging.getLogger(__name__)拿到的logger配置。如果''设置为固定值logger1,则下次导入必须设置成logging.getLogger('logger1')
        '': {
            # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
            'handlers': ['default', 'console'],
            'level': 'DEBUG',
            'propagate': False,  # 向上(更高level的logger)传递
        },
    },
}



def load_my_logging_cfg():
    logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
    logger = logging.getLogger(__name__)  # 生成一个log实例
    logger.info('It works!')  # 记录该文件的运行状态
    
    return logger


if __name__ == '__main__':
    load_my_logging_cfg()

4.2 log

import time
import logging
import my_logging  # 导入自定义的logging配置

logger = logging.getLogger(__name__)  # 生成logger实例


def demo():
    logger.debug("start range... time:{}".format(time.time()))
    logger.info("中文测试开始。。。")
    for i in range(10):
        logger.debug("i:{}".format(i))
        time.sleep(0.2)
    else:
        logger.debug("over range... time:{}".format(time.time()))
    logger.info("中文测试结束。。。")


if __name__ == "__main__":
    my_logging.load_my_logging_cfg()  # 在你程序文件的入口加载自定义logging配置
    demo()

Guess you like

Origin www.cnblogs.com/bowendown/p/11600587.html