Does hashlib support a file mode?

Phlip phlip2005 at
Wed Jul 6 23:07:47 CEST 2011

On Jul 6, 1:25 pm, Carl Banks <pavlovevide... at> wrote:

> We already know about this violation of the least surprise principle; most of us acknowledge it as small blip in an otherwise straightforward and clean language.

Here's the production code we're going with - thanks again all:

def file_to_hash(path, hash_type=hashlib.md5):

    hash = hash_type()

    with open(path, 'rb') as f:

        while True:
            s =  #  CONSIDER:  io.DEFAULT_BUFFER_SIZE
            if not s:  break

    return hash.hexdigest()

Note the fix also avoids comparing to None, which, as usual, is also
icky and less typesafe!

(And don't get me started about the extra lines needed to avoid THIS

        while s =


More information about the Python-list mailing list