Python comparing two files partially
Question: I have the two input file:

Input 1:

okay sentence  
two runway  
three runway  
right runway  
one pathway  

four pathway  
zero pathway  

Input 2 :

okay sentence  
two runway  
three runway  
right runway  
zero pathway  

one pathway  
four pathway  

I have used the following code:

    
    
    def diff(a, b):
    y = []
    for x in a:
        if x not in b:
            y.append(x)
        else:
            b.remove(x)
    return y
    
    with open('output_ref.txt', 'r') as file1:
       with open('output_ref1.txt', 'r') as file2:
        same = diff(list(file1), list(file2))
        print same
        print "\n"
    
    if '\n' in same:
      same.remove('\n')
    
    with open('some_output_file.txt', 'w') as FO:
      for line in same:
        FO.write(line)
    

And the expected output is :

one pathway  

zero pathway  

But the output I am getting an empty output for this. The problem is I don't
know how to store the content from the files to the list partially ,then
compare and finally read it back from there. Can someone help me in this
regard ??


Answer: It seems that if you just want to have the common text lines in both files,
sets would provide a good way. Something like this:

    
    
    content1 = set(open("file1", "r"))
    content2 = set(open("file2", "r"))
    diff_items = content1.difference(content2)
    

UPDATE: But is it so that the question is about difference in the same sense
as the `diff` utility? I.e. the order is important (looks like that with the
examples).

