Skip to content

Instantly share code, notes, and snippets.

@yangjuven
Created February 14, 2012 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangjuven/1825162 to your computer and use it in GitHub Desktop.
Save yangjuven/1825162 to your computer and use it in GitHub Desktop.
Get md5 of file without getting all content. It's very pythonic
#!/usr/bin/env python
import sys, hashlib
m = hashlib.md5()
with open(sys.argv[1], "rb") as fp:
for chunk in iter(lambda :fp.read(128 * m.block_size), ""):
m.update(chunk)
print m.hexdigest()
@yangjuven
Copy link
Author

juven@xxx:~$ time md5sum access_log-20120213 
348ee067cdbdf285c632c459d65298b1  access_log-20120213

real    0m35.074s
user    0m5.440s
sys 0m2.200s
juven@xxx:~$ vi md5.py
juven@xxx:~$ time python md5.py access_log-20120213 
348ee067cdbdf285c632c459d65298b1

real    0m34.478s
user    0m7.336s
sys 0m2.856s

效率看起来还不错哦。

@yangjuven
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment