Does hashlib support a file mode?

Peter Otten __peter__ at web.de
Wed Jul 6 19:06:13 CEST 2011


Phlip wrote:

>> - Open the file in binary mode.
> 
> I had tried open(path, 'rb') and it didn't change the "wrong" number.
> 
> And I added --binary to my evil md5sum version, and it didn't change
> the "right" number!
> 
> Gods bless those legacy hacks that will never die, huh? But I'm using
> Ubuntu (inside VMWare, on Win7, on a Core i7, because I rule), so that
> might explain why "binary mode" is a no-op.

Indeed. That part was a defensive measure mostly meant to make your function 
Windows-proof.

>> - Do the usual dance for default arguments:
>> def file_to_hash(path, m=None):
>> if m is None:
>> m = hashlib.md5()
> 
> Not sure why if that's what the defaulter does? I did indeed get an
> MD5-style string of what casually appeared to be the right length, so
> that implies the defaulter is not to blame...

The first call will give you the correct checksum, the second: not. As the 
default md5 instance remembers the state from the previous function call 
you'll get the checksum of both files combined. 




More information about the Python-list mailing list