Python的學習(三十) ---- Python實現檔案md5校正__Python

來源:互聯網
上載者:User

Linux下校正檔案MD5值,最簡單的方法就是執行md5sum命令
md5sum filename
原本打算用subprocess調用系統命令來擷取md5值,

import subprocess,shlexcmd = "md5sum filename"p = subprocess(shlex.split(cmd), stdout=subprocess.PIPE)print p.stdout.read()

不過python有內建的MD5模組hashlib,用起來簡單很多,
Python Hashlib模組的使用說明 http://docs.python.org/2/library/hashlib.html
fd = hashlib.md5() #擷取一個MD5密碼編譯演算法對象
fd.update("string") #指定需要加密的字串
fd.hexdigest() #擷取加密後的16進位字串

執行個體

 #!/usr/bin/env python            #coding : utf-8 3  4 import sys  import hashlib                                                    def md5sum(filename):                fd = open(filename,"r")     fcont = fd.r     fd.close()              fmd5 = hashlib.md5(fcont)     return fmd5                                               if __name__ == "__main__":           fmd5 = md5sum(sys.argv[1])     print fmd5.hexdigest()  

其中fmd5 = hashlib.md5(fcont)等同於
fmd5 = hashlib.md5(fcont)
fmd5.update(fcont)

需要注意的是,傳入 hashlib.md5() 的應該是 檔案內容而不是檔案名稱 ,這樣才是對檔案內容產生md5校正碼;
另外,調用了 hashlib.md5() 後返回的是一個對象,想要獲得linux下 md5sum 同樣的效果,還要調用一下 hexdigest() 方法。

但是,這個方法有點過於粗暴,當檢驗大檔案時,一次將所有檔案內容讀入記憶體,實在耗費較大,
網上給出執行個體http://blog.csdn.net/shanliangliuxing/article/details/10115397,
根據檔案塊長度,依次擷取檔案內容讀入記憶體,通過update()逐次更新校正值,

#!/usr/bin/env python 2 #coding : utf-8 3 import hashlib    def md5hex(word):     """ MD5密碼編譯演算法,返回32位小寫16進位符號     """      if isinstance(word, unicode):         word = word.encode("utf-8")     elif not isinstance(word, str):         word = str(word)     m = hashlib.md5()     m.update(word)     return m.hexdigest()   def md5sum(fname):     """ 計算檔案的MD5值     """     def read_chunks(fh):         fh.seek(0)         chunk = fh.read(8096)         while chunk:             yield chunk             chunk = fh.read(8096)         else: #最後要將遊標放迴文件開頭             fh.seek(0)     m = hashlib.md5()     if isinstance(fname, basestring) \             and os.path.exists(fname):         with open(fname, "rb") as fh:             for chunk in read_chunks(fh):                 m.update(chunk)     #上傳的檔案快取 或 已開啟的檔案流     elif fname.__class__.__name__ in ["StringIO", "StringO"] \             or isinstance(fname, file):         for chunk in read_chunks(fname):             m.update(chunk)     else:         return ""40     return m.hexdigest()

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.