The MD5 implementation is built into Python, which is the MD5 module, so it's easy to deal with the problem of comparing two files consistently. such as the following code fragment:
Import Os,sys,md5
F1 = open (' F:/1.txt ', ' R ')
F2 = open (' F:/1.txt ', ' R ')
Print md5.new (F1.read ()). Digest () = = Md5.new (F2.read ()). Digest ()
The above processing will have memory problems when dealing with larger files (such as hundreds of M files).
----------------------------------------------another way to implementCode: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)