tags:

views:

79

answers:

3

Hi, I need to compare two files and redirect the different lines to third file. I know using diff command i can get the difference . But, is there any way of doing it in python ? Any sample code will be helpful

+4  A: 

difflib

Ignacio Vazquez-Abrams
A: 
in1 = file("in1", "r") 
in2 = file("in2", "r") 
out = file("out", "r") 
do 
  str1 = in1.readline() 
  str2 = in2.readline() 
  if str1!=str2: out.writelines(str1+str2)  
  if not str1: break 
end 
in1.close(); in2.close(); out.close()
Riateche
+3  A: 

check out difflib

This module provides classes and functions for comparing sequences. It can be used for example, for comparing files, and can produce difference information in various formats, including HTML and context and unified diffs[...]

A command-line example in http://docs.python.org/library/difflib.html#difflib-interface

remosu