compute crc of file in python

user203547 picture user203547 · Nov 16, 2009 · Viewed 41.3k times · Source

I want to calculate the CRC of file and get output like: E45A12AC. Here's my code:

#!/usr/bin/env python 
import os, sys
import zlib

def crc(fileName):
    fd = open(fileName,"rb")
    content = fd.readlines()
    fd.close()
    for eachLine in content:
        zlib.crc32(eachLine)

for eachFile in sys.argv[1:]:
    crc(eachFile)

This calculates the CRC for each line, but its output (e.g. -1767935985) is not what I want.

Hashlib works the way I want, but it computes the md5:

import hashlib
m = hashlib.md5()
for line in open('data.txt', 'rb'):
    m.update(line)
print m.hexdigest()

Is it possible to get something similar using zlib.crc32?

Answer

kobor42 picture kobor42 · Mar 5, 2010

A little more compact and optimized code

def crc(fileName):
    prev = 0
    for eachLine in open(fileName,"rb"):
        prev = zlib.crc32(eachLine, prev)
    return "%X"%(prev & 0xFFFFFFFF)

PS2: Old PS is deprecated - therefore deleted -, because of the suggestion in the comment. Thank you. I don't get, how I missed this, but it was really good.