« »

Python 删除指定日期前的指定文件

  1. #-*- coding:gbk-*-
  2. #code by linuxany.com
  3. #version 1.01
  4.  
  5. import getopt,sys,string
  6. import os, datetime,time,glob
  7. from stat import *
  8. reload(sys)
  9.  
  10. sys.setdefaultencoding('utf-8')
  11. IFS="/" #路径分割符
  12. _delday = "7"
  13. _deldir = "/tmp"
  14. _delext = "log"

  1. def getargv():
  2.     if len(sys.argv) <2:
  3.         usage()
  4.         sys.exit(3) 
  5.     try:
  6.         opts, args = getopt.getopt(sys.argv[1:], "hy:d:e:", ["help", "day=", "dir=", "rar="]) 
  7.     except getopt.GetoptError:
  8.         usage() 
  9.         sys.exit(2) 
  10.     for o, a in opts:
  11.      if o in ("-y", "--day"):
  12.         global _delday
  13.         _delday = a
  14.      elif o in ("-d", "--dir"):
  15.         global _deldir
  16.         _deldir = a
  17.      elif o in ("-e", "--rar"):
  18.         global _delrar
  19.         _delrar = a
  20.      else:
  21.         usage() 
  22.         sys.exit() 
  23. def usage():
  24.     print "this program will delete specified files at created by N days."
  25.     print u"本程序用于删除指定目录下文件修改日期在指定时间前的指定后缀类型文件"
  26.     print " usage:"
  27.     print " -h, --help : this help screen"
  28.     print " [-y 7|--day=7] : delete files before 7 day"
  29.     print " [-e rar|--ext=log] : only delete filename extension like .log files"
  30.     print " : support * , will delete all files"
  31.     print " [-d /tmp|--dir=/tmp] : only delete /tmp dirctory's files"
  32.     print " Default parameter is delete /tmp all .rar files before 7 day"
  33.     print " Ex:"
  34.     print " dnd.exe -y 7 -d /usr/local/apache/logs/ -e log"
  35.     print " or"
  36.     print " dnd.exe --day=7 --dir=/usr/local/apache/logs/ --ext=log"
  37.  
  38. def erasefile(Eday,Edir,Erar):
  39.     print "Erase files program starting..."
  40.     logpath=Edir
  41.     count=False
  42.     fullpath=glob.glob(Edir+'/*.'+Erar)
  43.     for i in range(len(fullpath)):
  44.         t1 = time.gmtime(os.stat(fullpath[i])[ST_MTIME]) #get file's mofidy time
  45.         t11 = time.strftime('%Y-%m-%d',t1)
  46.         year,month,day=t11.split('-')
  47.         t111= datetime.datetime(int(year),int(month),int(day)) 
  48.         t2 = time.gmtime()
  49.         t22 = time.strftime('%Y-%m-%d',t2)
  50.         year,month,day=t22.split('-')
  51.         t222= datetime.datetime(int(year),int(month),int(day)) 
  52.         days = (t222-t111).days
  53.         if days>string.atof(Eday): # if over N days then remove file
  54.                 try:
  55.                         os.remove(fullpath[i])
  56.                         print fullpath[i] ,"deleted."
  57.                         count = True
  58.                         log=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+" remove "+fullpath[i]+" success \n"
  59.                 except:
  60.                         log=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+" remove "+fullpath[i]+" fail \n" 
  61.                 fTemp=open(logpath+IFS+"remove_file.log", 'a') 
  62.                 fTemp.write(log)
  63.     if count == False:
  64.         print "No file be deleted."
  65.     print "Erase files program End..."
  66.  
  67. if __name__ == '__main__'

您还可能感兴趣的内容

日志信息 »

该日志于2011-02-03 15:41由 admin 发表在Python分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »


返回顶部