使用python检查2个文件的新行独立标识的最佳方法

Kai*_*ann 2 python compare file

我试过了

filecmp.cmp(file1,file2)
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为除了换行符之外,文件是相同的.在filecmp或其他一些便利功能/库中是否有选项,或者我必须逐行读取这两个文件并进行比较?

And*_*Dog 5

我认为这样一个简单的便利功能应该可以完成这项工作:

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()))
Run Code Online (Sandbox Code Playgroud)