中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

常用python日期、日志、獲取內容循環的代碼片段是什么

發布時間:2021-12-04 17:47:24 來源:億速云 閱讀:111 作者:柒染 欄目:互聯網科技

這篇文章將為大家詳細講解有關常用python日期、日志、獲取內容循環的代碼片段是什么,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

近段時間對shell腳本和python進行了梳理,將一些腳本中常用的內容,考慮多種方法整理出來,形成有用的代碼片段,這樣就可以在需要的時候直接使用,也可以用于備忘和思考。
本次整理的代碼片段有: python中日期、時間常用獲取方法; 記錄處理日志的logging模塊使用;從目錄,文件,命名結果中,獲取循環條件進行循環。
我將這些有用的代碼片段整理在一個Python腳本中了,并且測試可用。
腳本內容如下:

  • #!/usr/bin/env python

  • #_*_coding:utf8_*_


  • #常用日期和時間

  • import datetime,time

  • today = 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 logging

  • import os


  • THISFILEPATH = 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 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))



  • ###從一個目錄中,取出所有文件名進行循環

  • from subprocess import call


  • File_Dir = "/tmp"

  • File_List = os.listdir(File_Dir)

  • for f in File_List:

  •     if( f[0] == '.' ):

  •         continue

  •     else:

  •         logging.info("{filename}".format(filename=f))


  • ###讀取一個文件的多行,進行循環

  • #文件讀取方法一:

  • file = open("/tmp/3.txt")

  • while 1:

  •     line = file.readline()

  •     if not line:

  •         break

  •     logging.info("this line is :{line}".format(line=line))


  • #文件讀取方法二:

  • import fileinput

  • for 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命令執行結果,進行循環

  • #獲取命令方法一:

  • import subprocess

  • shell_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()))


       上面的腳本,可使用 python 3.py 直接執行。
       腳本執行結果如下:

  • 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 critical




  • 2017-03-02 16:48:43 PM - 3.py - [line:46] INFO: now time is 2017-03-02 16:48:43

  • 2017-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.csv

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: proc_cpu_result

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: qaucli_022317-18_15_41.log

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: sfcbHttpSocket

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: 1.py

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: sfcbLocalSocket

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: 2.txt

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: null

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: 3.txt

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: 3.py

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: hsperfdata_root

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: updatehbaconf.sh.log

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: proc_cpu_detail.txt

  • 2017-03-02 16:48:43 PM - 3.py - [line:60] INFO: python_test_file.log

  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :1


  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :2


  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :3


  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :4


  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :5


  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :6


  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :7


  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :8


  • 2017-03-02 16:48:43 PM - 3.py - [line:69] INFO: this line is :9


  • 2017-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 :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 .

關于常用python日期、日志、獲取內容循環的代碼片段是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

绥德县| 昆山市| 二连浩特市| 大田县| 瑞金市| 临江市| 合作市| 高青县| 桓仁| 宜春市| 中阳县| 大悟县| 喀喇沁旗| 蓬溪县| 海晏县| 沙雅县| 台南市| 尚志市| 颍上县| 厦门市| 上高县| 邵武市| 兴城市| 北票市| 乐亭县| 南宫市| 西藏| 垫江县| 广丰县| 曲阳县| 陕西省| 方城县| 洪洞县| 大英县| 西青区| 中山市| 新竹市| 大田县| 新邵县| 舟曲县| 大兴区|