我的编程空间,编程开发者的网络收藏夹
学习永远不晚

常用python日期、日志、获取内容循环的代码片段是什么

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

常用python日期、日志、获取内容循环的代码片段是什么

这篇文章将为大家详细讲解有关常用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日期、日志、获取内容循环的代码片段是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

常用python日期、日志、获取内容循环的代码片段是什么

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

常用python日期、日志、获取内容循环的代码片段是什么

这篇文章将为大家详细讲解有关常用python日期、日志、获取内容循环的代码片段是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。近段时间对shell脚本和python进行了梳理,将一些脚本
2023-06-04

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录