0

I'm relatively new to python and I am using difflib to compare two files and I want to find all the lines that don't match. The first file is just one line so it is essentially comparing against all the lines of the second file. When using difflib, the results show the '-' sign in front of the lines that don't match and it doesn't show anything in front of the line that does match. (I thought it would show a '+'). For the lines that have a '-' in front, how can I just write those lines to a brand new file (without the '-' in front) ? Below is the code snippet I am using for the difflib. Any help is greatly appreciated.

f=open('fg_new.txt','r')

f1=open('out.txt','r')

str1=f.read()

str2=f1.read()

str1=str1.split()

str2=str2.split()

d=difflib.Differ()

diff=list(d.compare(str2,str1))

print ('\n'.join(diff))

AT2679
  • 1

2 Answers2

0

I have no idea about difflib but for comparing two file line by line you can try this. Start from here.

with open('fg_new.txt') as f, open('out.txt') as f1:
    for line1, line2 in zip(f, f1):
        if line1 != line2:
            #Do whatever you want to do with two lines.
Rahul
  • 10,830
  • 4
  • 53
  • 88
0

You may use below

set(str1).intersection(set(str2))

which will give you the difference of the two list.

Arun
  • 651
  • 2
  • 7
  • 21
  • Thanks all for the options. Further to the use of intersection() above, I actually used difference() and that helped. For reference, I used: set(str2).difference(set(str1)) which checked for whatever was in str2 but not in str1. This resulted in the desired lines being outputted. – AT2679 Jan 16 '18 at 04:28
  • Cheers.. Please mark the solution so that the post can be marked as "answered". – Arun Jan 16 '18 at 04:48