Home  >  Article  >  Backend Development  >  Python MD5 file generation code

Python MD5 file generation code

高洛峰
高洛峰Original
2017-01-14 14:13:581234browse

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)

For more articles related to Python MD5 file generation code, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn