- #!/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()
来源: http://www.bubuko.com/infodetail-3105679.html