美文网首页
Python 计算文件 CRC32 值

Python 计算文件 CRC32 值

作者: Anoyi | 来源:发表于2020-03-17 15:43 被阅读0次

依赖安装

# zlib 不是 python 包,需要使用如下方式安装
yum/apt-get install zlib-devel

计算方式

import zlib

def crc32(file_path):
    """
    计算文件 crc32 hash 值
    """
    with open(file_path, 'rb') as fh:
        hash = 0
        while True:
            s = fh.read(65536)
            if not s:
                break
            hash = zlib.crc32(s, hash)
        return "%08X" % (hash & 0xFFFFFFFF)

类似于 MD5 的 Hash 算法,校验文件

相关文章

网友评论

      本文标题:Python 计算文件 CRC32 值

      本文链接:https://www.haomeiwen.com/subject/jrmtyhtx.html