A simply newbie question about ndiff

David Goodger goodger at users.sourceforge.net
Sun Apr 21 23:01:12 EDT 2002


Neville Franks wrote:
> I thought that Tim's original ndiff compared files, but the one in difflib
> compares lists of strings. Does this mean I somehow need to read the files
> into lists of strings and then call ndiff.

Yes.  It's easy to turn a file into a list of strings.  Read with
afile.readlines(), or split a multi-line string with astring.splitlines(1)
(the 1 keeps the line endings).

> A short example of how to use
> ndiff to compare two files would be greatly appreciated.

Use the source, Luke!  There are painfully detailed examples in there.

Here's a quickie (untested, but oughta work)::

    import difflib
    list1 = open('file1').readlines()
    list2 = open('file2').readlines()
    diff = difflib.ndiff(list1, list2)
    print ''.join(diff)

-- 
David Goodger  <goodger at users.sourceforge.net>  Open-source projects:
  - Python Docutils: http://docutils.sourceforge.net/
  - The Go Tools Project: http://gotools.sourceforge.net/




More information about the Python-list mailing list