python腳本---清理過時文件

#!/usr/bin/python
import os,time

del_file = [] #定義一個空列表,將存放過時文件

class clean:

    def __init__(self,file_path):
        self.file_path = file_path

    def del_file(self):
        lf = list(os.listdir(self.file_path)) #將目標目錄下的全部文件存在列表lf中

        for i in range(len(lf)):
            file_date = os.stat(self.file_path + lf[i]).st_mtime #獲取文件的最後修改時間
            date_time = time.time()
            rt = (date_time - file_date)/60/60 #獲取最後修改時間和如今的時間間隔(小時)
            if rt < 1:
                del_file.append(lf[i]) #將時間間隔小於1小時的文件添加至列表del_file

    def file_remove(self):
        for j in range(len(del_file)): #刪除過時文件
            os.remove(self.file_path + del_file[j])

out_file = clean("/mnt/python/py/")
out_file.del_file()
print(del_file)
out_file.file_remove()
相關文章
相關標籤/搜索