檔案多了亂放, 突然有一天發現硬碟空間不夠了, 於是寫了個python腳本搜尋所有大於10MB的文件,看看這些大檔案有沒有重複的副本,如果有,全部列出,以便手動刪除
使用方式加上一個指定目錄的參數
例如python redundant_remover.py /tmp
主要用到了stat模組,os、sys系統模組
import os, sys #引入统计模块 from stat import * BIG_FILE_THRESHOLD = 10000000L dict1 = {} # filesize 做 key, filename 做 value dict2 = {} # filename 做 key, filesize 做 value 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 print "\n" if __name__=="__main__": treewalk(sys.argv[1]) printdict(dict2)