In Python, is there a concise way of comparing whether the contents of two text files are the same?

后端 未结 8 631
我在风中等你
我在风中等你 2020-11-29 01:51

I don\'t care what the differences are. I just want to know whether the contents are different.

8条回答
  •  时光说笑
    2020-11-29 02:24

    The low level way:

    from __future__ import with_statement
    with open(filename1) as f1:
       with open(filename2) as f2:
          if f1.read() == f2.read():
             ...
    

    The high level way:

    import filecmp
    if filecmp.cmp(filename1, filename2, shallow=False):
       ...
    

提交回复
热议问题