Buffer = 8192来 from the network, but do not know how to practice out.
From Hashlib import MD5 import time import OS def calMD5 (str): M = MD5 () m.update (str) return M.hexdig
EST () def calmd5forfile (file): Statinfo = os.stat (file) if int (statinfo.st_size)/(1024*1024) >= 1000:
Print "File size > 1000, move to Big File ..." return calmd5forbigfile (file) m = MD5 () f = open (file, ' RB ') M.update (F.read ()) F.close () return m.hexdigest () def calmd5forfolder (Dir,md5fi
Le): outfile = open (Md5file, ' W ') for root, subdirs, files in Os.walk (dir): For file in Files: Filefullpath = Os.path.join (root, file) "" "Print Filefullpath" "" Filerelpath = OS.PA Th.relpath (Filefullpath, dir) md5 = Calmd5forfile (Filefullpath) outfile.write (filerelpath+ ' +md5+ ' \ n ') outfile.close () def calmd5forbigfile (): M = MD5 () f = open (file, ' RB ') buffer = 8192 # why is 8192 | 8192 is fast tHan 2048 while 1:chunk = f.read (buffer) if not chunk:break m.update (chunk)
F.close () return m.hexdigest () if __name__ = = "__main__": #print calMD5 ("Hello world!") t = time.time () print calmd5forfile ("E:\\os\\ubuntu-11.04-desktop-i386.iso") print time.time ()-T T
= Time.time () print calmd5forbigfile ("E:\\os\\ubuntu-11.04-desktop-i386.iso") print time.time ()-T, "\ n" t = time.time () print calmd5forfile ("E:\\os\\ubuntu-12.04-desktop-amd64.iso") print time.time ()-t t = Ti Me.time () Print calmd5forbigfile ("E:\\os\\ubuntu-12.04-desktop-amd64.iso") print time.time ()-T, "\ n" t = Time.time () print calmd5forfile ("d:\\virtual machines\\ubuntu 64-bit\\ubuntu 64-bit-s001.vmdk") Print time.time (
)-T t = time.time () print calmd5forbigfile ("d:\\virtual machines\\ubuntu 64-bit\\ubuntu 64-bit-s001.vmdk") Print time.time ()-T, "\ n "#output #8b1085bed498b82ef1485ef19074c281 #2.57500004768 #8b1085bed498b82ef1485ef19074c281 #3.3410000 8011 # #128f0c16f4734c420b0185a492d92e52 #2 632999897 #128f0c16f4734c420b0185a492d92e52 #3.39100003242 # #File Size > 1000, move to big file ... #ec1fa4dc1b32569e9da7b4744548a9ef #5.40100002289 #ec1fa4dc1b32569e9da7b4744548a9ef # 5.42100000381