Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8961

Re: Does hashlib support a file mode?

From Peter Otten <__peter__@web.de>
Subject Re: Does hashlib support a file mode?
Date 2011-07-06 19:06 +0200
Organization None
References <453cc47b-b545-4bcf-91c0-2efe2aae37bd@v11g2000prn.googlegroups.com> <mailman.704.1309969573.1164.python-list@python.org> <b317226a-8008-4177-aaa6-3fdc30125eea@e20g2000prf.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.709.1309971973.1164.python-list@python.org> (permalink)

Show all headers | View raw


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. 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Does hashlib support a file mode? Phlip <phlip2005@gmail.com> - 2011-07-06 08:59 -0700
  Re: Does hashlib support a file mode? Peter Otten <__peter__@web.de> - 2011-07-06 18:26 +0200
    Re: Does hashlib support a file mode? Phlip <phlip2005@gmail.com> - 2011-07-06 09:49 -0700
      Re: Does hashlib support a file mode? Peter Otten <__peter__@web.de> - 2011-07-06 19:06 +0200
        Re: Does hashlib support a file mode? Phlip <phlip2005@gmail.com> - 2011-07-06 10:38 -0700
          Re: Does hashlib support a file mode? Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-06 13:42 -0500
      Re: Does hashlib support a file mode? Chris Torek <nospam@torek.net> - 2011-07-06 17:54 +0000

csiph-web