python学习:hashlib模块使用
#!/usr/bin/env python
import sys
import hashlib
def md5sum(f):
m = hashlib.md5()
with open(f) as fd:
while True:
data = fd.read(4096)
if data:
m.update(data)
else:
break
return m.hexdigest()
if __name__=='__main__':
try:
print md5sum(sys.argv[1])
except IndexError:
print "%s follow a argument" %__file__
[root@web10 day2]# python 1_md5sum.py /etc/passwd
cad05d25487852dc3816a83d19544e3e
[root@web10 day2]# python 1_md5sum.py
1_md5sum.py follow a argument
[root@web10 day2]# md5sum /etc/passwd
cad05d25487852dc3816a83d19544e3e /etc/passwd
用python里的hexdigest() 实现 md5sum 的命令