check if files are the same on Windows

Shane Geiger sgeiger at
Mon Mar 19 17:55:08 CET 2007

In the unix world, 'fc' would be like diff.

Python example of checksumming files with the MD5 module.

In Python 2.5, the hashlib module would be preferable/more elegant.

import md5

import string, os
r = lambda f: open(f, "r").read()
def readfile(f,strip=False): return (strip and stripper(r(f))) or r(f)
def writefile(f, data, perms=750): open(f, "w").write(data) and 
os.chmod(f, perms)

def get_md5(fname):
    hash =
    contents = readfile(fname)
    value = hash.digest()
    return (fname, hash.hexdigest())

import glob

for f in glob.glob('*'):
    print get_md5(f)

> A crude way to check if two files are the same on Windows is to look
> at the output of the "fc" function of cmd.exe, for example
> def files_same(f1,f2):
>     cmnd    = "fc " + f1 + " " + f2
>     return ("no differences" in popen(cmnd).read())
> This is needlessly slow, because one can stop comparing two files
> after the first difference is detected. How should one check that
> files are the same in Python? The files are plain text.

Shane Geiger
IT Director
National Council on Economic Education
sgeiger at  |  402-438-8958  |

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list