获取文件的MD5码等

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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

import sys
import os



def getFileHashCode(filename,type):
    if not os.path.exists(filename):
        return [True,"找不到这个文件:"+filename]
    else:
        try:
            nameSpace=__import__("hashlib")
        except:
            return [True,"你的系统中缺少hashlib模块"]
        
        try:
            func = getattr(nameSpace,type)
        except:
            return [True,"没有找到这个加密方法:"+type]
        sha1 = func()
        file = open(filename,"rb")
        while True:
            data = file.read(4096)
            if not data:
                break
            sha1.update(data)
        return [False,sha1.hexdigest()]


if __name__=="__main__":
    try:
        fileName=sys.argv[1]
        try:
            hash=sys.argv[2]
        except:
            hash="sha1"
        array=sys.argv[1].split("\\")
        temp=getFileHashCode(fileName,hash)
        if(temp[0]):
	    print temp[1]
        else:
            print array[len(array)-1]+"文件的"+hash+"码是:"+temp[1]
    except:
        print"使用方法:python hash.py fileName hash"