计算某个目录下所有文件的MD5值

#!/usr/bin/env python
#-*- coding:utf-8 -*-

''' 计算某个目录下所有文件的MD5值 '''

import os
import sys
import hashlib

def md5sum(path):
    for i in os.listdir(path):
        md5 = hashlib.md5()
        files = os.path.join(path, i)
        if os.path.isfile(files):
            with open(files) as fd:
                while True:
                    data = fd.read(4096)
                    if data:
                        md5.update(data)
                    else:
                        print md5.hexdigest(), files
                        break
        else:
            md5sum(files)

if __name__ == '__main__':
    md5sum(sys.argv[1])
[root@localhost ~]$ python 1.py /tmp/studypy/
2ee2e03f60f140642770462984923398 /tmp/studypy/get_all_pid.py
593e49c086fd76e246ef810759c3698d /tmp/studypy/practice2.py
3d4fe4c24eddc1cb263f594bad6e5138 /tmp/studypy/list_all_files.py
4ec7bfa31d49ef34205e444194b54495 /tmp/studypy/practice1.py

 

 

 

 

 

     

posted @ 2019-01-21 14:41  孔雀东南飞  阅读(2469)  评论(0编辑  收藏  举报