#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import shutil
import time
need_delete_dir = []
# 扫描指定目录下的所有文件夹及文件
for path, dirs, files in os.walk('/Users/chengpei/IdeaProjects'):
for filename in files:
# 扫描到所有文件夹名称为target,并且里面的jar文件的目录
if path.endswith('target') and filename.endswith('.jar'):
t = os.path.getctime(path + '/' + filename)
days = (time.time() - t) / 3600 / 24
if days > 3:
# 编译超过三天的文件删除
print(path, filename, (time.time() - t) / 3600 / 24)
need_delete_dir.append(path)
for path in set(need_delete_dir):
shutil.rmtree(path)
使用crontab在设置定时任务,每天执行即可
0 10 * * * python3 delete_target.py
评论区