Python Training Day6 Essays

Source: Internet
Author: User
Tags timedelta

Today I've talked about some of the Python's practical local tool modules


Loggin Module for logging

#!/usr/bin/env python#--*--coding:utf-8--*--import logginglogging.basicconfig (filename= ' Wgwtest.log ' ,  #filename代表记录的日志文件名称                      format= '% (asctime) s - % (name) s - % (levelname) s  -% (module) s:  % (message) s ',                      #日志格式参数   include modify time   user name of launcher   log alarm level   Program name   Alarm information for logging                      datefmt= '%y-%m-%d %h:%m:%s %p ',                      #日志中asctime的时间格式                       level=logging. DEBUG)                       #设置日志等级, set to print only this level and alarms above this level                       #默认的等级是logging. warning# Alarm Message (message) s content logging.debug ( ' Debug ') logging.info (' info ') logging.warning (' Warning ') logging.error (' Error ') logging.critical (' critical ')

The printed log content is

2015-12-04 16:21:15 pm-root-debug-wgwtest:debug2015-12-04 16:21:15 pm-root-info-wgwtest:info2015-12-04 16:21 : pm-root-warning-wgwtest:warning2015-12-04 16:21:15 pm-root-error-wgwtest:error2015-12-04 16:21:15 pm-r Oot-critical-wgwtest:critical

In the parameters of the log format, there are a lot of things you can do to set the

% (Levelno) S: Print the value of the log level

% (levelname) S: Print log level name

% (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]

% (filename) s: Prints the current name of the executing program

% (funcName) s: Print the current function of the log

% (Lineno) d: Print the current line number of the log

% (asctime) s: Time to print logs

% (thread) d: Print thread ID

% (threadname) s: Print thread name

% (process) d: Print process ID

% (message) s: Print log information

Log print level from high to low

CRITICAL  = 50 FATAL  = CRITICAL ERROR  = 40 WARNING  = 30 WARN  = WARNING INFO  = 20 DEBUG  = 10 NOTSET  = 0


Time Module

Import timeprint time.strftime ('%y-%m-%d ', Time.localtime ()) #格式化实现本地当前时间
2015-12-04 18:21:18
Import Timeprint Time.asctime (Time.localtime ()) #以文字形式显示的日期
Fri Dec 04 18:33:10 2015


DateTime module

Import Datetimeprint Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s ') #获取当前时间
2015-12-04 18:37:42
Import Datetimeprint Datetime.datetime.now ()-Datetime.timedelta (days=5) #获取5天前的日期2015-11-29 18:44:33.063000
Import Datetimeprint Datetime.datetime.now () + Datetime.timedelta (minutes=30) #获取30分钟后的时间2015-12-04 19:18:35.263000


Shutil Module

For advanced operations on files, folders, you can copy files or folders and compress folders


This article from "Thunderbolt Tofu" blog, declined reprint!

Python Training Day6 Essays

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.