A simply newbie question about ndiff

Emile van Sebille emile at fenx.com
Sun Apr 21 23:13:13 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. 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.
>

David pointed you to python22/tools/scripts/difflib.py.  In it you'll
find this example of how to use it:

def fopen(fname):
    try:
        return open(fname, 'r')
    except IOError, detail:
        return fail("couldn't open " + fname + ": " + str(detail))

# open two files & spray the diff to stdout; return false iff a problem
def fcompare(f1name, f2name):
    f1 = fopen(f1name)
    f2 = fopen(f2name)
    if not f1 or not f2:
        return 0

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

    return 1


You could then say something like:

fcompare(r'c:\CrunchyFrog.txt', r'c:\CrunchyFfruug.txt')

Assuming-you-were-casting-Peter-Sellers-in-a-skit-ly y'rs,


--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list