pythonfilecompare

Best way to check new-line-independent-identity of 2 files with python


I tried

filecmp.cmp(file1,file2)

but it doesn't work since files are identically except for new line characters. Is there an option for that in filecmp or some other convenience function/library or do I have to read both files line by line and compare those?


Solution

  • I think a simple convenience function like this should do the job:

    from itertools import izip
    
    def areFilesIdentical(filename1, filename2):
        with open(filename1, "rtU") as a:
            with open(filename2, "rtU") as b:
                # Note that "all" and "izip" are lazy
                # (will stop at the first line that's not identical)
                return all(myprint() and lineA == lineB
                           for lineA, lineB in izip(a.xreadlines(), b.xreadlines()))