ndiff

Ian Bicking ianb at colorstudy.com
Wed Jul 23 20:49:43 EDT 2003


On Wed, 2003-07-23 at 18:55, Bryan wrote:
> 3.  is there a simple method that just returns true or false whether two
> files are different or not?  i was hoping that ndiff/compare would return an
> empty list if there was no difference,  but that's not the case.  i ended up
> using a simple: if file1.read() == file2.read(): but there must be a smarter
> faster way.

Maybe something like:

def areDifferent(file1, file2):
    while 1:
        data1, data2 = file1.read(1000), file2.read(1000)
        if not data1 and not data2:
            return True
        if data1 != data2:
            return False


You still have to go through the entire file if you really want to be
sure.  If you use filenames, of course, you can take some shortcuts:

def filesDiffer(filename1, filename2):
    if os.stat(filename1).st_size != os.stat(filename2).st_size:
        return False
    else:
        return areDifferent(open(filename1), open(filename2)

You could also try a quick comparison from somewhere not at the
beginning (using .seek(pos)), if you think it is likely that files will
have common headers.  But you'd still have to scan the entire file to be
sure.

  Ian







More information about the Python-list mailing list