Python: Calculate large file MD5 value __python

Source: Internet
Author: User
Tags md5

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.