Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 21st, 2004, 06:15 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old May 21st, 2004, 06:40 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
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

Reply With Quote
  #3  
Old May 21st, 2004, 07:36 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Compare files

I shall try it now
Thank you dave

This site is great

Reply With Quote
  #4  
Old May 21st, 2004, 07:59 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Compare File

Quote:
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

Reply With Quote
  #5  
Old May 21st, 2004, 09:07 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
Quote:
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

Reply With Quote
  #6  
Old May 21st, 2004, 09:27 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Compaer files

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap