使用python检查2个文件的新行独立身份的最佳方法 [英] Best way to check new-line-independent-identity of 2 files with python

查看:72
本文介绍了使用python检查2个文件的新行独立身份的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了

filecmp.cmp(file1,file2)

,但是它不起作用,因为文件相同,除了换行符。在filecmp或其他便利功能/库中是否可以选择该选项,还是必须逐行读取两个文件并进行比较?

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?

推荐答案

我认为像这样的简单便捷函数就可以完成这项工作:

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()))

这篇关于使用python检查2个文件的新行独立身份的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆