基于此脚本:
#!/usr/bin/python
# run by crontab
# removes any files in /tmp/ older than 7 days
import os, sys, time
from subprocess import call
now = time.time()
cutoff = now - (7 * 86400)
files = os.listdir("/tmp")
for xfile in files:
if os.path.isfile( "/tmp/" + xfile ):
t = os.stat( "/tmp/" + xfile )
c = t.st_ctime
# delete file if older than a week
if c < cutoff:
os.remove("/tmp/" + xfile)
我们可以根据修改时间删除路径中的文件,但是如何根据修改时间删除其他文件夹中的文件夹?
这意味着主文件夹中有很多文件夹,但我们需要保留主文件夹和子文件夹,并且只删除修改时间早于特定时间的文件夹。