python版本为:2.7
# -*- coding: utf-8 *-*
import os,time,shutil,datetime def rmdir(deldir,N): dellist=os.listdir(deldir) deldate= datetime.timedelta(days=N) now = datetime.datetime.now() for i in dellist: os.chdir(deldir) ctime = datetime.datetime.fromtimestamp(os.path.getctime(i)) if ctime < (now - deldate): #print ("%s rm dir :%s" %(now,i)) if os.path.isfile(i): os.remove(i) else: shutil.rmtree(i) #Timed C--logs deletion #Delete the directory at midnight every day and keep the last few days
if __name__=="__main__":" rmdir('C:logsAdf.NumberServer',7) rmdir('C:logsAdf.NumberServer',7)