清除距今天7天的日志

#!/usr/bin/env python
#encoding=utf-8
"""
清空最后修改时间跑今天7天以前的所有文件
"""
#指定监控的路径
path="/home/maolingzhi/remove_logs/aa"
import os,time,stat,datetime
for elem in os.listdir(path):
  try:
    pa = os.path.join(path,elem)
    fs = os.stat(pa)
    ct = time.ctime(fs[stat.ST_ATIME])
    ti = datetime.datetime.strptime(ct, "%a %b %d %H:%M:%S %Y")
    if (datetime.datetime.now() - ti).days > 7:
      os.remove(pa)
  except:
    pass

原文地址:https://www.cnblogs.com/lexus/p/3299514.html