在Python中,可以使用以下方法來比較兩個文件的不同點:
with open("file1.txt", "r") as file1, open("file2.txt", "r") as file2:
lines1 = file1.readlines()
lines2 = file2.readlines()
diff_lines = []
for i in range(len(lines1)):
if lines1[i] != lines2[i]:
diff_lines.append(i+1)
print(diff_lines)
with open("file1.txt", "r") as file1, open("file2.txt", "r") as file2:
content1 = file1.read()
content2 = file2.read()
diff_chars = []
for i in range(len(content1)):
if content1[i] != content2[i]:
diff_chars.append(i)
print(diff_chars)
這樣,你就可以得到兩個文件的不同點,可以是不同的行或不同的字符位置。請注意,以上方法假設兩個文件的行數或字符數是相同的。如果不相同,需要進行額外的處理。