A simply newbie question about ndiff

Tim Peters tim.one at comcast.net
Sun Apr 21 23:50:55 EDT 2002


[Neville Franks]
> Ok thanks. I've installed 2.2 and ndiff is available but I'm
> still confused.  I thought that Tim's original ndiff compared files,
> but the one in difflib compares lists of strings.

Tools/scripts/ndiff.py still exists, and still compares files.  If that's
all you want to do, just run it -- no need to program anything.  Or if you
waht to see how to use difflib to do it yourself, again
Tools/scripts/ndiff.py is the answer:  in Python 2.2, ndiff.py is a tiny
program that invokes difflib facilities to do all the hard work.

> Does this mean I somehow need to read the files into lists of strings
> and then call ndiff. A short example of how to use ndiff to compare two
> files would be greatly appreciated.

See function fcompare() in 2.2's ndiff.py.  The part that compares and
prints is 4 lines, including reading the files <wink>:

    a = f1.readlines(); f1.close()
    b = f2.readlines(); f2.close()
    for line in difflib.ndiff(a, b):
        print line,






More information about the Python-list mailing list