比较python中的两个文件

时间:2015-08-12 23:54:49

标签: python

我有以下代码。我已经检查了stackoverflow上的其他链接,但它们比我的稍微复杂一些。

现在我的文本文件有hello(在file1中)和hell(在file2中)作为数据。

我相信我的逻辑是正确的,但我收到以下错误

TypeError: object of type '_io.TextIOWrapper' has no len()

我哪里错了?

def compareString(line1,line2): #sub function to compare strings of files
    i=0 #initial index
    while line1[i]==line2[i]: #compare each line until they are equal
        i=i+1
    if line1[i]!=line2[i]: #if unequal
        print('Mismatch at character ',i,line1[i]) #print error message

def compareMain(): #
    file1=input('Enter the name of the first file: ') #input file1 name
    file2=input('Enter the name of the second file: ') #input file2 name

    fp1=open(file1,'r') #open file1, reading mode
    fp2=open(file2,'r') #open file2, reading mode
    for line1 in range(len(fp1)): #Getting each line of file1
        for line2 in range(len(fp2)): #Getting each line of file2
            compareString(line1,line2) #Call compare function
    fp1.close() #Close file1
    fp2.close() #Close file2

compareMain() #Execute

4 个答案:

答案 0 :(得分:1)

我会这样做:

def compare_files():
    file1=input('Enter the name of the first file: ') #input file1 name
    file2=input('Enter the name of the second file: ') #input file2 name
    fp1=open(file1,'r') #open file1, reading mode
    fp2=open(file2,'r') #open file2, reading mode
    if (fp1.read() == fp2.read()):
        print("Files are the same")
    else:
        print("Files are not the same")

compare_files()

方法.read()将返回文件的内容。我们获取两个文件的内容,然后我们比较这些文件的内容。

答案 1 :(得分:0)

您不需要使用range(len(fp1))。您可以直接使用fp1。这应该可以解决错误。

def compareString(line1,line2): #sub function to compare strings of files
    i=0 #initial index
    while line1[i]==line2[i]: #compare each line until they are equal
        i=i+1
    if line1[i]!=line2[i]: #if unequal
        print('Mismatch at character ',i,line1[i]) #print error message

def compareMain(): #
    file1=input('Enter the name of the first file: ') #input file1 name
    file2=input('Enter the name of the second file: ') #input file2 name

    fp1=open(file1,'r') #open file1, reading mode
    fp2=open(file2,'r') #open file2, reading mode
    for line1 in fp1: #Getting each line of file1
        for line2 in fp2: #Getting each line of file2
            compareString(line1,line2) #Call compare function
    fp1.close() #Close file1
    fp2.close() #Close file2

compareMain() #Execute

答案 2 :(得分:0)

正如Tris所说,我建议使用 difflib https://docs.python.org/2/library/difflib.html)。以下是您可以用来启动的代码段:

import difflib
import sys

file1=sys.argv[1] 
file2=sys.argv[2]

line1 = open(file1).readlines()
line2 = open(file2).readlines()

line1_idx = 0
line2_idx = 0

for l1 in line1:
    l1 = l1.rstrip()
    line2_idx = 0
    for l2 in line2:
        l2 = l2.rstrip()
        diff = difflib.SequenceMatcher(None, l1, l2)
        for tag, i1, i2, j1, j2 in diff.get_opcodes():
            if((tag == "delete") or (tag == "replace") or (tag == "insert")):
                print("Mismatch file1-line%d file2-line%d, line1-index[%d:%d] line2-index[%d:%d]" % (line1_idx, line2_idx, i1, i2, j1, j2))
        line2_idx += 1
    line1_idx += 1

答案 3 :(得分:0)

在我意识到提问者想要一个差异,而不仅仅是一个相同或不相同的检查之前,我开始这个答案,但我认为这个问题可能是那些想要那个的人找到的,所以在这里。虽然main()函数仅适用于两个文件,但核心代码可以使用任意数量的文件。无论哪种方式,它只会检查不匹配的第一行。

#
# Python 2/3 compatibility
#

from __future__ import print_function

try:
    from itertools import izip as zip  # Python 2
except ImportError:
    pass  # Python 3

try:
    input = raw_input  # Python 2
except NameError:
    pass  # Python 3


#
# You can leave out everything above if you're on Python 3
#

def all_equal(first_item, *items):
    return all(item == first_item for item in items)


def iterables_are_identical(*iterables):
    return all(all_equal(*tup) for tup in zip(*iterables))


def files_are_identical(*files, **kwargs):
    mode = kwargs.get('mode', 'r')

    open_files = []
    try:
        for f in files:
            open_files.append(open(f, mode))

        return iterables_are_identical(*open_files)
    finally:
        for handle in open_files:
            handle.close()


def main():
    msg = 'Enter the name of the %s file: '
    file_a = input(msg % 'first')
    file_b = input(msg % 'second')

    if files_are_identical(file_a, file_b):
        print('Files are identical')
    else:
        print('Files are NOT identical')


if __name__ == '__main__':
    main()