使用python删除N天前的文件
2024-10-06 11:16:39
python版本为:2.7
import os
import sys
import time # Sets how many days old files are deleted
DAYS_N = 7
# To delete the path and the following subfiles
PATH = r'C:\inetpub\logs\LogFiles' def deletefile(PATH):
for eachfile in os.listdir(PATH):
filename = os.path.join(PATH, eachfile)
if os.path.isfile(filename):
lastmodifytime = os.stat(filename).st_mtime
# Sets how many days old files are deleted
endfiletime = time.time() - 3600 * 24 * DAYS_N
if endfiletime > lastmodifytime:
# To remove the following comment is to delete the.log suffix file
# Comment is delete path under all files do not match
if filename[-4:] == ".log":
os.remove(filename)
print "del %s success!!!" % filename
# If it is a directory, the current function is called recursively
elif os.path.isdir(filename):
deletefile(filename) if __name__ == '__main__':
deletefile(PATH) time.sleep(1)
print ('Deleting completed,success')
最新文章
- php通过判断来源主机头进行防盗链
- JS事件-让网页交互
- JavaScript正则表达式小记
- codeforces 486B.OR in Matrix 解题报告
- poj1979
- 《DDNS服务器的搭建和案例解决方法》
- [Oracle AR]Territory Flexfield
- HDU4521+线段树+dp
- weblogic 12c 配置jvm的内存大小
- 原生JS写Ajax的请求函数
- Android 网络框架--Retrofit
- JQ 替换节点
- 在C语言中基本数据类型所占的字节数
- CDN页面刷新接口定义[高升]
- Unity3d导出场景地图寻路
- June 5. 2018 Week 23rd Tuesday
- MT【248】$f(x)=\dfrac{1}{x-1}+\dfrac{1}{x-b}$的性质
- 检测浏览器(BOM)以及地址栏网址的API
- python脚本参数传递
- list页面-按照choice筛选丶传condition过滤筛选项丶筛选与显示同步