六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 30|回复: 0

delete files using python

[复制链接]

升级  45.33%

30

主题

30

主题

30

主题

秀才

Rank: 2

积分
118
 楼主| 发表于 2013-1-28 18:59:17 | 显示全部楼层 |阅读模式
import os, sys,datetime,time
from stat  import *
path='c:\\test\\'
path2='c:\\test\\'
filelist=[]
filelist=os.listdir(path)
for i in range(len(filelist)):
        t1 = time.gmtime(os.stat(path+filelist)[ST_MTIME])  #get file's mofidy time
        t11 =  time.strftime('%Y-%m-%d',t1)
        year,month,day=t11.split('-')
        t111= datetime.datetime(int(year),int(month),int(day))        
        t2 = time.gmtime()
        t22 =  time.strftime('%Y-%m-%d',t2)
        year,month,day=t22.split('-')
        t222= datetime.datetime(int(year),int(month),int(day))        
        days =  (t222-t111).days
        if days>5 :  # if over 5 days then remove file
                try:
                        os.remove(path+filelist)
                        log=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"  remove "+path+filelist+"  success \n"
                except:
                        log=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"  remove "+path+filelist+"  fail \n"               
                fTemp=open(path2+"remove_file.log", 'a')        
                fTemp.write(log)
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表