Python reads the file by row and removes the line break "\ n" (check your eyesight, compare documents differently) __python

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.