CRC32 on files

Alex Martelli aleax at aleax.it
Fri Jan 4 05:29:48 EST 2002


"Adonis Vargas" <deltapigz at telocity.com> wrote in message
news:3c355398$1_1 at nopics.sjc...
> how do i run a CRC32 checksum on a file with python? or do i have the


For example:

def crc32_of_file(filepath):
    from zlib import crc32
    fileobj = open(filepath,'rb')
    current = 0
    while 1:
        buffer = fileobj.read(8192)
        if not buffer: break
        current = crc32(buffer, current)
    fileobj.close()
    return current

or, if you know the file ain't TOO huge, and are willing to trust
"automatic closure" of the opened file (explicit close is better...):

import zlib
thevalueyouwant = zlib.crc32(open(filepath,'rb').read())


Alex






More information about the Python-list mailing list