Python : 計算大檔案MD5值__Python

來源:互聯網
上載者:User

buffer = 8192 來自網路,但不知道是怎麼實踐出來的。


from hashlib import md5import timeimport os def calMD5(str):    m = md5()    m.update(str)         return m.hexdigest() 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,MD5File):    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.path.relpath(filefullpath, dir)            md5 = calMD5ForFile(filefullpath)            outfile.write(filerelpath+' '+md5+"\n")    outfile.close() def calMD5ForBigFile(file):    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 = time.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.34100008011##128f0c16f4734c420b0185a492d92e52#2.632999897#128f0c16f4734c420b0185a492d92e52#3.39100003242##File size > 1000, move to big file...#ec1fa4dc1b32569e9da7b4744548a9ef#5.40100002289#ec1fa4dc1b32569e9da7b4744548a9ef#5.42100000381 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.