[轉載]python指令碼刪除一定時間以外的檔案,

來源:互聯網
上載者:User

[轉載]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();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.