当前位置: 编程技术>python
Python MD5文件生成码
来源: 互联网 发布时间:2014-09-04
本文导语: import md5 import sys def sumfile(fobj): m = md5.new() while True: d = fobj.read(8096) if not d: break m.update(d) return m.hexdigest() def md5sum(fname): if fname == '-': ret = sumfile(sys.stdin) else: try: f = file(fname, 'rb') except: return 'Failed to open file' ret = sumfile(f) f.close() ret...
import md5
import sys
def sumfile(fobj):
m = md5.new()
while True:
d = fobj.read(8096)
if not d:
break
m.update(d)
return m.hexdigest()
def md5sum(fname):
if fname == '-':
ret = sumfile(sys.stdin)
else:
try:
f = file(fname, 'rb')
except:
return 'Failed to open file'
ret = sumfile(f)
f.close()
return ret
if __name__ == '__main__':
for fname in sys.argv[1:]:
print '%32s %s' % (md5sum(fname), fname)
import sys
def sumfile(fobj):
m = md5.new()
while True:
d = fobj.read(8096)
if not d:
break
m.update(d)
return m.hexdigest()
def md5sum(fname):
if fname == '-':
ret = sumfile(sys.stdin)
else:
try:
f = file(fname, 'rb')
except:
return 'Failed to open file'
ret = sumfile(f)
f.close()
return ret
if __name__ == '__main__':
for fname in sys.argv[1:]:
print '%32s %s' % (md5sum(fname), fname)