File1=input ("Enter the filename you want to open:") + ". txt"
f1=open (File1, "R") for
line1 in F1.readlines ():
Line1=line1.strip ( "\ n")
print (line1)
Extensions: Compares the two files entered by the user and, if different, displays the position of the line number and the first different character at all different points.
def file_compare (file1,file2):
with open (File1) as F1, open (File2) as F2:
count=0 #统计行数
differ=[] #统计不一样的数量 For
line1 in F1:
line1=line1.strip ("\ n")
Line2=f2.readline ()
line2=line2.strip ("\ n")
count +=1
if Line1!=line2:
differ.append (count) return
differ
file1=input ("Enter the first file name you want to compare:") + ". txt "
file2=input (" Enter the first file name you want to compare: ") +". txt "
differ=file_compare (file1,file2)
If Len (differ) ==0:
print ("Two files in exactly the same")
else:
print ("Two files have a total of"%d "Different:"%len (differ)) for every in
differ:
print (" Line%d not the same "%each)
Reference:
http://bbs.fishc.com/forum.php?mod=viewthread&tid=45487&extra=page%3D1%26filter%3Dtypeid%26typeid% 3d398