Skip to content

Instantly share code, notes, and snippets.

@xingkaixin
Created September 1, 2013 07:32
Show Gist options
  • Save xingkaixin/6402890 to your computer and use it in GitHub Desktop.
Save xingkaixin/6402890 to your computer and use it in GitHub Desktop.
From http://www.cnblogs.com/wangchy0927/p/3291304.html 删除文件夹内规定时间内的文件
#-*-coding=gbk -*-
import os
import time
def listDir(fileDir):
for eachFile in os.listdir(fileDir):
if os.path.isfile(fileDir+"/"+eachFile): #如果是文件,判断最后修改时间,符合条件进行删除
ft = os.stat(fileDir+"/"+eachFile);
ltime = int(ft.st_mtime); #获取文件最后修改时间
#print "文件"+path+"/"+eachFile+"的最后修改时间为"+str(ltime);
ntime = int(time.time())-3600*3; #获取现在时间减去3h
if ltime<=ntime :
print "我要删除文件"+fileDir+"/"+eachFile;
os.remove(fileDir+"/"+eachFile); #删除3小时前的文件
elif os.path.isdir(fileDir+"/"+eachFile) : #如果是文件夹,继续递归
listDir(fileDir+"/"+eachFile);
if __name__ == '__main__':
path = "E:/offlinefiles"; #规定目录
while True : #持续
time.sleep(600); #减少资源利用率 600s秒一次
print "3600s wake up";
listDir(path);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment