首页 > 编程语言 > 详细

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

时间:2015-05-20 20:23:35      阅读:299      评论:0      收藏:0      [点我收藏+]
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

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

原文:http://www.cnblogs.com/dongchi/p/4517923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!