Python 实现指定目录下 删除指定大小的文件

import os, sys
from stat import *
BIG_FILE_THRESHOLD = 6000L #1000000L
dict1 = {}  # 
dict2 = {}   # 
def treewalk(path):
  try:
    for i in os.listdir(path):
      mode = os.stat(path+"/"+i).st_mode
      if S_ISDIR(mode) <> True:
        filename = path+"/"+i
        filesize = os.stat(filename).st_size
        if filesize < BIG_FILE_THRESHOLD:
          if filesize in dict1:           
            dict2[filename] = filesize
            dict2[dict1[filesize]]=filesize
          else:
            dict1[filesize] = filename         
      else:
        treewalk(path+"/"+i)
  except WindowsError:
    pass
def printdict(finaldict):
  for i_size in finaldict.values():
    print i_size
    for j_name in finaldict.keys():
      if finaldict[j_name] == i_size:
        print j_name
	os.remove(j_name)
    print "\n"
if __name__=="__main__":
  treewalk(sys.argv[1])
  printdict(dict2)

  使用方法 remove c:/xx

posted @ 2015-05-20 18:49  anything good  阅读(699)  评论(0编辑  收藏  举报
孤 's 博客