标签:break lease hashlib try blog ges 默认 data walk
1 #!/usr/bin/env python 2 3 import os 4 import sys 5 import hashlib 6 7 try: 8 dirpath = sys.argv[1] 9 except IndexError as e: 10 sys.exit(‘need one dirctory‘) 11 if os.path.isfile(dirpath): 12 sys.exit(‘Please input a directory‘) 13 14 pathobj = os.walk(dirpath) 15 16 def md5sum(f): 17 m = hashlib.md5() 18 with open(f) as fd: 19 while True: 20 data = fd.read(4096) #每次读取一部分,避免因为文件过大,造成对内存的压力 21 if data: 22 m.update(data) 23 else: 24 break 25 return m.hexdigest() 26 27 for roots,dirs,files in pathobj: 28 for file in files: 29 filename = os.path.join(roots,file) 30 md5 = md5sum(filename) 31 print md5,filename
标签:break lease hashlib try blog ges 默认 data walk
原文地址:http://www.cnblogs.com/dachenzi/p/6793912.html