1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
import os import hashlib from time import clock as now
def getmd5(filename): file_txt = open(filename, 'rb').read() md5 = hashlib.md5() md5.update(file_txt)
return md5.hexdigest()
path = '.' all_md5 = [] total_file = 0 delete_file = 0
def del_dump(path): global total_file global delete_file
for parent, dirnames, filenames in os.walk(path): for filename in filenames: total_file += 1 real_path = os.path.join(parent, filename)
filemd5 = getmd5(real_path) if filemd5 in all_md5: delete_file += 1 os.remove(real_path) print 'remove:',real_path
else: all_md5.append(filemd5)
def del_emptydir(path): if os.path.isdir(path): for p in os.listdir(path): d = os.path.join(path, p) if(os.path.isdir(d) == True): del_emptydir(d) if not os.listdir(path): os.rmdir(path) print 'remove dir:', path
start = now()
del_dump(path)
end = now()
time_last = end - start
print 'total file: ', total_file print 'delete file: ', delete_file print 'cost time: ', time_last, 's'
|
近期评论