Python - Compare 2 files and output differences

丶灬走出姿态 提交于 2019-12-01 02:38:39

This is working for me:

def compare(File1,File2):
    with open(File1,'r') as f:
        d=set(f.readlines())


    with open(File2,'r') as f:
        e=set(f.readlines())

    open('file3.txt','w').close() #Create the file

    with open('file3.txt','a') as f:
        for line in list(d-e):
           f.write(line)

You need to compare the readlines set and find out lines that are not present in file2. You can then append these lines to the new file.

s baskaravishnu
with open("H:/Ast/Hpa.java", encoding="utf8") as f:
    with open("G:/Soft_install/Hpa.java", encoding="utf8") as fe:
        for line in f:
            for linefe in fe:
                if (line != linefe):
                    print(line)
                    break
                else:
                    break
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!