How do I compare files?

Larry Bates larry.bates at websafe.com`
Tue Jul 22 21:47:05 EDT 2008


Clay Hobbs wrote:
> I am making a program that (with urllib) that downloads two jpeg files
> and, if they are different, displays the new one.  I need to find a way
> to compare two files in Python.  How is this done?
> 
> -- Ratfink
> 

Use md5 to calculate checksum:

import md5

md5file1 = md5.md5(open(filename1).read()).hexdigest()
md5file2 = md5.md5(open(filename2).read()).hexdigest()

if md5file1 != mdfile2:
     #
     # Do whatever you want
     #

-Larry



More information about the Python-list mailing list