zoukankan      html  css  js  c++  java
  • 操作LOG文件-删除log目录下,所有的空文件-删除5天前的文件

    # 1logs目录下,有一部分文件是空的
    # 1、删除log目录下,所有的空文件
    # 2、删除5天前的文件

    # 需求分析:
    # 1os.walk()获取到所在以.log结尾的文件
    # 2、判断文件的大小,os.getsize()
    # 3、先从文件件名里面获取到文件的日期,然后把日期转成时间戳
    # 4、再获取到当年的时间戳,时行比较

    import os,time,datetime
    def strToTimestamp(str=None,format='%Y%m%d%H%M%S'):
    # 20180421165643
    #默认返回当前时间戳
    if str: #如果传了时间的话
    tp = time.strptime(str,format) #格式化好的时间,转成时间元组
    res = time.mktime(tp)#再转成时间戳
    else:
    res = time.time() #默认取当前的时间戳
    return int(res)

    def clean_log(path):
    for abs_path,dir,file in os.walk(path):
    for f in file:
    if f.endswith('.log'):
    full_path = os.path.join(abs_path,f) #文件的绝对理解
    f_date = f.split('_')[-1].split('.')[0] #文件名里面的日期
    f_date_time_stamp = strToTimestamp(f_date,'%Y-%m-%d') #把文件名里面的日期转成时间戳
    five_day = str(datetime.date.today() + datetime.timedelta(-5)) #获取到5天前的日期
    five_day_time_stamp = strToTimestamp(five_day, '%Y-%m-%d') # 再把天前的日期转成时间戳
    if os.path.getsize(full_path)==0 or f_date_time_stamp<five_day_time_stamp:
    os.remove(full_path)
    clean_log('logs')


  • 相关阅读:
    树的直径 学习笔记
    SDOJ 3742 黑白图
    【SDOJ 3741】 【poj2528】 Mayor's posters
    SDOJ 3740 Graph
    SDOJ 3696 Tree
    SDOJ 1195 Zhenhuan
    又一次受刺激后的发奋
    html_表单form中的input类型大集合
    js_表格的增删改
    JS_拖拽窗口的实现
  • 原文地址:https://www.cnblogs.com/jiadan/p/9033441.html
Copyright © 2011-2022 走看看