Looking for File comparison utility that produces actual differences

Jeff McNeil jeff at jmcneil.net
Wed Sep 3 21:01:20 CEST 2008


On Sep 3, 2:40 pm, dudeja.ra... at gmail.com wrote:
> Hi,
>
> I looking for a file comparison utility in Python that works like
> 'diff' command in Unix and 'comp' in Windows.
> The present 'cmd'  in filecmp module only presents output in the form
> of 1 or 0 i.e whether the 2 files differ or not?
>
> So, I'm lookin for something that generates actual differences and
> moreover it is cross platform as well. As I need that same thing in
> both my windows and linux box.
>
> Even if it is a utlility please suggest.
>
> Thanks and regards,
> Rajat

What about the difflib module?

[jeff at marvin test]$ cat file_one
abcd
1234
-
[jeff at marvin test]$ cat file_two
abcd
12345
-
[jeff at marvin test]$


[jeff at marvin test]$ python
Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import difflib
>>> for i in difflib.context_diff(open("file_one").readlines(), open("file_two").readlines()):
...     print i,
...
***
---
***************
*** 1,3 ****
  abcd
! 1234
  -
--- 1,3 ----
  abcd
! 12345
  -
>>>

Also, see http://docs.python.org/lib/module-difflib.html.

-Jeff



More information about the Python-list mailing list