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 = md5.new()
   contents = readfile(fname)
   hash.update(contents)
   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
[EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to