同じ数の行を含む 2 つのファイルがあります。
"file1.txt" contains following lines:
Attitude is a little thing that makes a big difference
The only disability in life is a bad attitude
Abundance is, in large part, an attitude
Smile when it hurts most
"file2.txt" contains:
Attitude is a little thing that makes a big difference
Everyone has his burden. What counts is how you carry it
Abundance is, in large part, an attitude
A positive attitude may not solve all your problems
2つのファイルを1行ずつ比較したいのですが、2つのファイル間で行の不一致があれば、
print "mismatch in line no: 2"
print "mismatch in line no: 4" #in this case lineno: 2 and lineno: 4 varies from second file
試してみましたが、file2 の行とは異なる file1 の行しか印刷できません。一致しない行の行番号を印刷できませんか??
My code:
with open("file1.txt") as f1:
lineset = set(f1)
with open("file2.txt") as f2:
lineset.difference_update(f2)
for line in lineset:
print line