Thread: Compaer files

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10

    Compaer files


    Hello all

    I am trying to compare 2 files and print the (contents) difference do anyone know how to ?

    file1
    file2
    file3=difference
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Yes it is possible - in fact in Python it is astonishingly easy.

    However the answer depends on the type of files you are passing in, and the format of the difference you are expecting out. I will assume that you want to compare text files, and get the output as a context diff then you can do it easily with the difflib module that was added in Python 2.1. This can show you the lines that were different, and even where in the line the difference is.
    For example, from the docs:

    Code:
    >>> text1 = '''  1. Beautiful is better than ugly.
    ...   2. Explicit is better than implicit.
    ...   3. Simple is better than complex.
    ...   4. Complex is better than complicated.
    ... '''.splitlines(1)
    >>> text2 = '''  1. Beautiful is better than ugly.
    ...   3.   Simple is better than complex.
    ...   4. Complicated is better than complex.
    ...   5. Flat is better than nested.
    ... '''.splitlines(1)
    >>> d = Differ()
    >>> result = list(d.compare(text1, text2))
    >>> import sys
    >>> sys.stdout.writelines(result)
        1. Beautiful is better than ugly.
    -   2. Explicit is better than implicit.
    -   3. Simple is better than complex.
    +   3.   Simple is better than complex.
    ?     ++
    -   4. Complex is better than complicated.
    ?            ^                     ---- ^
    +   4. Complicated is better than complex.
    ?           ++++ ^                      ^
    +   5. Flat is better than nested.
    The only potential issue is that the differ needs arrays of strings, so you need to read the entire files into memory. This may cause problems if the files are huge.

    Here is a program that will do what you ask (I think - I have not tested it).

    Code:
    import sys
    import difflib
    
    first = file(sys.argv[1]).readlines()
    second file(sys.argv[2].readlines()
    
    diff = difflib.ndiff(first, second)
    for line in diff: 
       print rstrip(line)
    There is a more robust version of this program that comes with Python - see python/tools/scripts/ndiff.py.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10

    Compare files


    I shall try it now
    Thank you dave

    This site is great
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10

    Compare File


    Hello Dave
    I tried and i am getting the following error

    Traceback (most recent call last):
    File "F:/Pyth2/FileDirbinary10.py", line 11, in -toplevel-
    print rstrip(line)
    NameError: name 'rstrip' is not defined

    I don't know if my coding is correct is there any other way to open a file from the netwoked server and copy or append to my local disk. Please help i am a beginner.


    Here is my code

    Code:
    import os,sys,difflib
    os.chdir('/')
    os.chdir('\\\\server1')
    openconsole=open('CONSOLE.LOG','rb',)
    readconsole=openconsole.read()
    os.chdir('c:\\')
    local=open('console.log','rb',)
    local1=local.read()
    diff=difflib.ndiff(readconsole,local1)
    for line in diff:
        print rstrip(line)
    
    local.close()
    openconsole.close()
    Thanks
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by pyton
    I tried and i am getting the following error

    Traceback (most recent call last):
    File "F:/Pyth2/FileDirbinary10.py", line 11, in -toplevel-
    print rstrip(line)
    NameError: name 'rstrip' is not defined
    Whoops, my mistake. It should have been

    Code:
    print line.rstrip()
    I did say it was untested code!

    Dave - The Developers' Coach
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10

    Compare Files


    Sorry

    I tried again but i am not getting what i want

    I want to disply only the difference between file1 and file2.
    (say file one has 10 lines and file2 has same as file1 but 3 extra lines and I want the programme to pick only the extra 3 lines not everything from file1 and file2)

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo