我有如下代码。我查看了其他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
.read()
呢?这将给您整个文件的内容。您可以将此内容与第二个文件的内容进行比较。您也可以使用.read()
获取第二个文件的内容。 - golobitch