python對比2個檔案內容
阿新 • • 發佈:2019-02-13
file1 = "D:\\1.txt"
file2 = "D:\\2.txt"f_diff = "D:\\diff.txt"
# ---------- 對比檔案內容,輸出差異
f1 = open(file1, "r")
f2 = open(file2, "r")
file1 = f1.readlines()
file2 = f2.readlines()
f1.close()
f2.close()
outfile = open(f_diff, "w")
flag = 0
outfile.write("file1獨有的資料:\n")
for i in file1:
if i not in file2:
outfile.write(i)
flag = 1
outfile.write("file2獨有的資料:\n")
for i in file2:
if i not in file1:
outfile.write(i)
flag = 1
outfile.close()
if flag == 1:
print "資料存在差異,請仔細核對!"