Python删除指定时间的文件

时间:2022-03-10 21:53:57
import os
import time
import sys
from xml.dom import minidom, Node
from xml.dom.minidom import parse,parseString
from stat import *
import xml.etree.ElementTree as ET

#删除1day前的日志
def DelLog(filepath):
if not os.path.isdir(filepath) and not os.path.isfile(filepath):
return False

if os.path.isfile(filepath):
#判断最后修改时间
ltime = os.stat(filepath)[ST_MTIME]; #获取文件最后修改时间
ntime = int(time.time())-86400 #获取现在时间减去1day
if ltime<=ntime :
os.remove(filepath)
elif os.path.isdir(filepath):
for item in os.listdir(filepath):
DelLog(os.path.join(filepath,item))

DelLog('D:test\')

 然后用一个批处理执行。

@echo off
cd /d %~dp0
python \getmdtime.py

echo. & pause

 可以使用windows任务计划,定期自动运行执行该脚本。