首页 > 代码库 > [转载]python脚本删除一定时间以外的文件

[转载]python脚本删除一定时间以外的文件

 

 1 import os;
 2 import sys;
 3 import time;
 4 
 5 class DeleteLog:
 6     def __init__(self,filename,days):
 7         self.filename=filename;
 8         self.days=days;
 9 
10     def delete(self):
11         if os.path.exists(self.filename)==False:
12             print(self.filename+  is not exists!!)
13         elif os.path.isfile(self.filename):
14             print(self.filename);
15         elif os.path.isdir(self.filename):
16             print(self.filename +  is a path!);
17             for i in [os.sep.join([self.filename,v]) for v in os.listdir(self.filename)]:
18                 if self.compare_file_time(i) and (os.path.isfile(i)):
19                     os.remove(i);
20                     print(i+ is removed!);
21 
22     def compare_file_time(self,file):
23         time_of_last_mod=os.path.getatime(file);
24         days_between=(time.time()-time_of_last_mod)/(24*60*60);
25         if days_between>self.days:
26             return True;
27         return False;
28 
29 
30 if __name__==__main__:
31     path=/u01/app/diag/rdbms/orcl/orcl/trace;
32     obj=DeleteLog(path,5);
33     obj.delete();

 

[转载]python脚本删除一定时间以外的文件