pythonファイルmd 5の取得

935 ワード

#!/usr/bin/env python
import sys;
import hashlib;

def GetFileMd5(strFile):
    file = None;
    bRet = False;
    strMd5 = "";
    
    try:
        file = open(strFile, "rb");
        md5 = hashlib.md5();
        strRead = "";
        
        while True:
            strRead = file.read(8096);
            if not strRead:
                break;
            md5.update(strRead);
        #read file finish
        bRet = True;
        strMd5 = md5.hexdigest();
    except:
        bRet = False;
    finally:
        if file:
            file.close()

    return [bRet, strMd5];

if "__main__" == __name__:
    strPath = raw_input("please input get md5 file:");
    print(GetFileMd5(strPath));