Common python date, log, get content Loop code snippet

Source: Internet
Author: User
Tags python script timedelta

The shell script and Python have been combed in the near time, and some of the common content in the script has been sorted out to form useful code fragments, which can be used directly when needed, and can be used for forgetting and thinking.
This collation of the code snippet is: Python in the date, time commonly used to get the method, record processing log logging module use, from the directory, file, named results, get the loop condition to loop.
I've organized these useful snippets in a Python script, and the tests are available.
The script reads as follows:

#!/usr/bin/env python#_*_coding:utf8_*_# Common date and time import datetime,timetoday =  Datetime.date.today () Yesterday = datetime.date.today ()  - datetime.timedelta (Days=1) Tomorrow = datetime.date.today ()  + datetime.timedelta (Days=1) Today_nyr = int ( Datetime.datetime.strftime (today,  '%y%m%d ')) Yesterday_nyr = int (Datetime.datetime.strftime ( yesterday,  '%y%m%d ')) Tomorrow_nyr = int (Datetime.datetime.strftime (tomorrow , '%y%m%d ')) Time.strftime ('%y-%m-%d %h:%m:%s ',  time.localtime (Time.time ()))  print  "now time  Is {time} ". Format (Time=time.strftime ('%y-%m-%d %h:%m:%s ',  time.localtime (Time.time ()))) print   "Normal type , today is {today} , yesterday is {yesterday}  , tomorrow is {tommorrow} . ". Format (today=today,yesterday=yesterday,tommorrow=tomorrow) print  "Nyr style , today is {today} , yesterday is {yesterday} , tommrrow is { tommorrow} . ". Format (TODAY=TODAY_NYR,YESTERDAY=YESTERDAY_NYR,TOMMORROW=TOMORROW_NYR) #logging. info ("Now time is  {time} ". Format (Time=time.strftime ('%y-%m-%d %h:%m:%s ',  time.localtime (Time.time ()))) # Logging.info ("normal type , today is {today} , yesterday is { yesterday} , tomorrow is {tommorrow} . ". Format (today=today,yesterday=yesterday,tommorrow=tomorrow)) #logging. info ("Nyr style , today  is {today} , yesterday is {yesterday} , tommrrow is { tommorrow} . ". Format (today=today_nyr,yesterday=yesterday_nyr,tommorrow=tomorrow_nyr) # # #程序日志模块logging使用import   Loggingimport osthisfilepath = os.path.dirname (Os.path.realpath (__file__)) logfile =  ' {path}/python_test_file.log '. Format (path=thisfilepath) logGing.basicconfig (level=logging. debug,                     format= '% (asctime) s - % (filename) s - [line:% (Lineno) d] % (levelname) s: % (message) s ',                     datefmt= '%y-%m-%d %h:%m:%s %p ',                     filename=logfile,                       #level =10,                     filemode= ' a ') logging.info ("this is a info.\n") Logging.warn ( "this is a warning.\n") logging.error ("this is a error.\n") #logging. Log ("THIS&NBsp;is a log. \n\n ") logging.debug (" this is a debug. \n\n ") logging.critical ( "this is a critical \n\n\n") Logging.info ("Now time is {time}". Format (time= Time.strftime ('%y-%m-%d %h:%m:%s ',  time.localtime (Time.time ())))) Logging.info ("Normal type  , today is {today} , yesterday is {yesterday} , tomorrow  is {tommorrow} . ". Format (Today=today,yesterday=yesterday,tommorrow=tomorrow)) Logging.info ("Nyr style , today  is {today} , yesterday is {yesterday} , tommrrow is { tommorrow} . ". Format (TODAY=TODAY_NYR,YESTERDAY=YESTERDAY_NYR,TOMMORROW=TOMORROW_NYR) # # # #从一个目录中, take out all file names for looping from  subprocess import callfile_dir =  "/tmp" File_list = os.listdir (File_Dir) for  F in file_list:    if ( f[0] ==  '. ')  ):  &Nbsp;      continue    else:         logging.info ("{filename}". Format (Filename=f)) # # #读取一个文件的多行, Loop # File Read method one: File = open ("/tmp/3.txt") while 1:    line = file.readline ()     if  not line:        break    logging.info ( "This line is :{line}". Format (Line=line)) #文件读取方法二: Import fileinputfor line in  fileinput.input ("/tmp/3.txt"):     logging.info ("This line is :{line}   . ". Format (line=line)) #文件读取方法三: File = open ("/tmp/3.txt") for line in file:     logging.info ("THIS LINE IS :{LINE}  ."). Format (line=line) # # # #获取一个shell命令执行结果, Loop # Get command method one:import subprocessshell_cmd =  "Df -h  | awk  ' {print $1} ' "p  = subprocess. Popen ("{cmd}". Format (Cmd=shell_cmd),  shell=true, stdout=subprocess. PIPE) Out = p.stdout.readlines () for line in out:    print  Line.strip ()     logging.info (" FILE SYSTEM IS :{LINE}  .". Format (Line=line.strip ())) #获取命令方法二: For line in subprocess. Popen ("df -h | awk  ' {print $1} '", Shell=true,stdout=subprocess. PIPE). Stdout.readlines ():     print line.strip ()     logging.info ("  file system is :{line}  . ". Format (Line=line.strip ()))


The above script can be executed directly using Python 3.py.
The script execution results are as follows:

2017-03-02 16:48:43 pm - 3.py - [line:38] info: this is a  info.2017-03-02 16:48:43 pm - 3.py - [line:39] warning: this  is a warning.2017-03-02 16:48:43 PM - 3.py - [line:40]  Error: this is a error.2017-03-02 16:48:43 pm - 3.py - [line : 42] debug: this is a debug.2017-03-02 16:48:43 pm - 3.py -  [line:43] critical: this is a critical2017-03-02 16:48:43 pm -  3.py - [line:46] info: now time is 2017-03-02 16:48:432017-03-02  16:48:43 PM - 3.py - [line:47] INFO: normal type ,  today is 2017-03-02 , yesterday is 2017-03-01 , tomorrow is  2017-03-03 .2017-03-02 16:48:43 pm - 3.py - [line:48] info: nyr style , today  is 20170302 , yesterday is 20170301 , tommrrow is 20170303  .2017-03-02 16:48:43 pm - 3.py - [line:60] info: 40001501_ 20170302.csv2017-03-02 16:48:43 pm - 3.py - [line:60] info: proc_cpu_ Result2017-03-02 16:48:43 pm - 3.py - [line:60] info: qaucli_022317-18 _15_41.log2017-03-02 16:48:43 pm - 3.py - [line:60] info:  Sfcbhttpsocket2017-03-02 16:48:43 pm - 3.py - [line:60] info: 1. py2017-03-02 16:48:43 pm - 3.py - [line:60] info:  Sfcblocalsocket2017-03-02 16:48:43 pm - 3.py - [line:60] info: 2. Txt2017-03-02 16:48:43 pm - 3.py -&nBsp [line:60] info: null2017-03-02 16:48:43 pm - 3.py - [line:60]  Info: 3.txt2017-03-02 16:48:43 pm - 3.py - [line:60] info: 3. Py2017-03-02 16:48:43 pm - 3.py - [line:60] info: hsperfdata_ root2017-03-02 16:48:43 pm - 3.py - [line:60] info:  updatehbaconf.sh.log2017-03-02 16:48:43 pm - 3.py - [line:60] info:  proc_cpu_detail.txt2017-03-02 16:48:43 pm - 3.py - [line:60] info:  python_test_file.log2017-03-02 16:48:43 pm - 3.py - [line:69] info:  this line is :12017-03-02 16:48:43 pm - 3.py - [line:69]  Info: this line is :22017-03-02 16:48:43 pm - 3.py - [line : 69] info: this line is :32017-03-02 16:48:43 pm - 3.py - [line:69] info: this line is  :42017-03-02 16:48:43 pm - 3.py - [line:69] info: this line  is :52017-03-02 16:48:43 pm - 3.py - [line:69] info: this  line is :62017-03-02 16:48:43 pm - 3.py - [line:69] info:  THIS LINE IS :72017-03-02 16:48:43 PM - 3.PY - [LINE:69]  info: this line is :82017-03-02 16:48:43 pm - 3.py - [ line:69] info: this line is :92017-03-02 16:48:43 pm - 3.py  - [line:74] info: this line is :1  .2017-03-02 16:48:43 pm  - 3.py - [line:74] info: this line is :2  .2017-03-02  16:48:43 pm - 3.py - [line:74] info: this line is :3  .2017-03-02  16:48:43 pm - 3.py - [line:74] info: this line is :4   .2017-03-02 16:48:43 pm - 3.py - [line:74] info: this  LINE IS :5  .2017-03-02 16:48:43 PM - 3.PY - [LINE:74]  info: this line is :6  .2017-03-02 16:48:43 pm - 3.py  - [line:74] info: this line is :7  .2017-03-02 16:48:43  pm - 3.py - [line:74] info: this line is :8  . 2017-03-02 16:48:43 pm - 3.py - [line:74] info: this line is  :9  .2017-03-02 16:48:43 PM - 3.py - [line:79] INFO:  This line is&nbsP;:1  .2017-03-02 16:48:43 pm - 3.py - [line:79] info: this  LINE IS :2  .2017-03-02 16:48:43 PM - 3.PY - [LINE:79]  info: this line is :3  .2017-03-02 16:48:43 pm - 3.py  - [line:79] info: this line is :4  .2017-03-02 16:48:43  pm - 3.py - [line:79] info: this line is :5  . 2017-03-02 16:48:43 pm - 3.py - [line:79] info: this line is  :6  .2017-03-02 16:48:43 PM - 3.py - [line:79] INFO:  This line is :7  .2017-03-02 16:48:43 pm - 3.py - [line :79] info: this line is :8  .2017-03-02 16:48:43 pm -  3.py - [line:79] info: this line is :9  .2017-03-02 16:48:43 pm -  3.py - [line:90] info:  file system is :filesystem  . 2017-03-02 16:48:43 pm - 3.py - [line:90] info:  file system  IS :/DEV/SDA2  .2017-03-02 16:48:43 PM - 3.PY - [LINE:90]  INFO:  file system is :tmpfs  .2017-03-02 16:48:43 PM  - 3.py - [line:90] info:  file system is :/dev/sda1  . 2017-03-02 16:48:43 pm - 3.py - [line:90] info:  file system  IS :/DEV/SDA5  .2017-03-02 16:48:43 PM - 3.PY - [LINE:96]  info:  file system is :filesystem  .2017-03-02 16:48:43 pm  - 3.py - [line:96] info:  file system is :/dev/sda2  .2017-03-02  16:48:43 pm - 3.py - [line:96] info:  file system is : tmpfs  .2017-03-02 16:48:43 pm - 3.py - [line:96] info:   file system is :/dev/sda1  .2017-03-02 16:48:43 pm - 3.py  - [line:96] info:  file system is :/dev/sda5  .


This article is from the "Yumushui column" blog, be sure to keep this source http://yumushui.blog.51cto.com/6300893/1902715

Common python date, log, get content Loop code snippet

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.