[Tutor] Different md5 values for files under Unix and Python?

Clay Shirky clay at shirky.com
Thu Jan 15 14:15:29 EST 2004


So I'm hashing some files, and I get different outputs for

os.popen("md5 filename") and hash.update("filename"); digest=hash.digest()

and I can't figure out what I'm doing wrong. The two md5's should produce
the same hash, right?

Test below

#!/usr/bin/env python

# Import
import sys, os
import md5
hash = md5.md5()

# Unix way
unix_md5 = os.popen("md5 -r book.txt")

# Python way
hash.update("book.txt")   # hash the file
digest = hash.hexdigest() # hex digest

# Print comparison
print digest, "book.txt"
for value in unix_md5:
    print value
#-----

which outputs

543117d4fc8b2ac471a88a1baa1f2b59 book.txt
6a2b6248246f2ec5682688c63c477f1e book.txt

Puzzled.

-clay




More information about the Tutor mailing list