比较python中的两个文件

比较python中的两个文件,python,Python,我有以下代码。我已经检查了stackoverflow上的其他链接,但是它们比我的稍微复杂一些 现在,我的文本文件有hello(在文件1中)和hell(在文件2中)作为数据 我相信我的逻辑是正确的,但我得到以下错误 TypeError: object of type '_io.TextIOWrapper' has no len() 我哪里做错了 def compareString(line1,line2): #sub function to compare strings of files

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

现在,我的文本文件有
hello
(在文件1中)和
hell
(在文件2中)作为数据

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

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

您不需要使用
范围(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

我会这样做:

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()
将返回文件的内容。我们得到两个文件的内容,然后比较这些文件的内容。

正如Tris提到的,我建议使用difflib()。以下是您可以用来开始的代码段:

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

我开始回答这个问题之前,我意识到提问者想要的是一个差异,而不仅仅是一个相同或不相同的检查,但我想这个问题可能会被那些确实想要的人发现,所以就在这里。虽然
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()

的确如此。感谢您的快速回复为什么不使用
.read()
?这将为您提供文件的全部内容。您可以将此内容与第二个文件的内容进行比较。你也可以通过
.read()
@golobich获得,我也会尝试这种方法。对于Python和编程而言,我仍然是新手。您可能会考虑使用FiffLB()来进行文件比较,因为如果您的一个文件缺少第一行,其余的行与另一个文件相同,那么您所做的方法可以显示出所有行的不同。difflib用法的代码示例位于。如果您只想知道文件的内容是否相同,那么比较校验和是有效的,filecmp()也是有效的。