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

后端 未结 8 528
我在风中等你
我在风中等你 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:16

    For larger files you could compute a MD5 or SHA hash of the files.

    0 讨论(0)
  • 2020-11-29 02:17

    This is a functional-style file comparison function. It returns instantly False if the files have different sizes; otherwise, it reads in 4KiB block sizes and returns False instantly upon the first difference:

    from __future__ import with_statement
    import os
    import itertools, functools, operator
    
    def filecmp(filename1, filename2):
        "Do the two files have exactly the same contents?"
        with open(filename1, "rb") as fp1, open(filename2, "rb") as fp2:
            if os.fstat(fp1.fileno()).st_size != os.fstat(fp2.fileno()).st_size:
                return False # different sizes ∴ not equal
            fp1_reader= functools.partial(fp1.read, 4096)
            fp2_reader= functools.partial(fp2.read, 4096)
            cmp_pairs= itertools.izip(iter(fp1_reader, ''), iter(fp2_reader, ''))
            inequalities= itertools.starmap(operator.ne, cmp_pairs)
            return not any(inequalities)
    
    if __name__ == "__main__":
        import sys
        print filecmp(sys.argv[1], sys.argv[2])
    

    Just a different take :)

    0 讨论(0)
  • 2020-11-29 02:19
    
    f = open(filename1, "r").read()
    f2 = open(filename2,"r").read()
    print f == f2
    
    
    
    0 讨论(0)
  • 2020-11-29 02:22

    If you're going for even basic efficiency, you probably want to check the file size first:

    if os.path.getsize(filename1) == os.path.getsize(filename2):
      if open('filename1','r').read() == open('filename2','r').read():
        # Files are the same.
    

    This saves you reading every line of two files that aren't even the same size, and thus can't be the same.

    (Even further than that, you could call out to a fast MD5sum of each file and compare those, but that's not "in Python", so I'll stop here.)

    0 讨论(0)
  • 2020-11-29 02:23
    from __future__ import with_statement
    
    filename1 = "G:\\test1.TXT"
    
    filename2 = "G:\\test2.TXT"
    
    
    with open(filename1) as f1:
    
       with open(filename2) as f2:
    
          file1list = f1.read().splitlines()
    
          file2list = f2.read().splitlines()
    
          list1length = len(file1list)
    
          list2length = len(file2list)
    
          if list1length == list2length:
    
              for index in range(len(file1list)):
    
                  if file1list[index] == file2list[index]:
    
                       print file1list[index] + "==" + file2list[index]
    
                  else:                  
    
                       print file1list[index] + "!=" + file2list[index]+" Not-Equel"
    
          else:
    
              print "difference inthe size of the file and number of lines"
    
    0 讨论(0)
  • 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):
       ...
    
    0 讨论(0)
提交回复
热议问题