使用python删除指定文件夹及子文件,保留多少天
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:\logs\Adf.NumberServer',7) rmdir('C:\logs\Adf.NumberServer',7)