我有兩個文本文件,名字是one.txt,同時two.txt 在此時就把one.txt存盤,內容是如何比較和合並在Python兩個文件
AAA
BBB
CCC
DDD
在two.txt,內容是
DDD
EEE
我希望有一個Python代碼,以確定包含two.txt都出現在此時就把one.txt存盤或不 如果存在的話手段,不要做任何事情,但如果two.txt內容不存在手段,它應該附加到one.txt
我想在此時就把one.txt存盤一個輸出
AAA
BBB
CCC
DDD
EEE
代碼:
file1 = open("file1.txt", "r")
file2 = open("file2.txt", "r")
file3 = open("resultss.txt", "w")
list1 = file1.readlines()
list2 = file2.readlines()
file3.write("here: \n")
for i in list1: for j in list2:
if i==j: file3.write(i)
請直接在問題中編輯您的代碼。 – timgeb
請將代碼添加到您的問題並縮進四個空格以保留格式;註釋並不適用於此,特別是對於像Python這樣的空白相關語言:) –
生成的文件是否需要特定的順序?在你的例子中,你的兩個源文件被排序 - 是假定的嗎?可能所有三個文件同時在內存中(所以文件不是很大)? –