User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 423,206 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,751 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 444 | Replies: 4
Reply
Join Date: Apr 2008
Posts: 4
Reputation: sreelatha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sreelatha sreelatha is offline Offline
Newbie Poster

How to compare two files and save difference in thrid file

  #1  
May 19th, 2008
Hi,

I have a requirement that i have two files, one is input file and another is log file(both will contain the same data mostly). I need to compare these two files and if any difference found then those lines should be write to another file(3rd file).

I done googling, but I am able to find only two files comparison which returns true or false.

Has any one have idea? please help me.

thx & rgds,
sreelatha
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 116
Reputation: Freaky_Chris is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Junior Poster

Re: How to compare two files and save difference in thrid file

  #2  
May 19th, 2008
Simple read the use file.readlines() on both files to give you 2 arrays

so something like this

  1. f1 = open("file1.txt", "r")
  2. f2 = open("file2.txt", "r")
  3.  
  4. fileOne = f1.readlines()
  5. fileTwo = f2.readlines()
  6. f1.close()
  7. f2.close()
  8. outFile = open("results.txt", "w")
  9. x = 0
  10. for i in fileOne:
  11. if i != fileTwo[x]:
  12. outFile.write(i+" <> "+fileTwo[x])
  13. x += 1
  14.  
  15. outFile.close()

something along those lines

Chris
Last edited by Freaky_Chris : May 19th, 2008 at 12:51 pm.
Reply With Quote  
Join Date: May 2008
Location: Toronto
Posts: 37
Reputation: kdoiron is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kdoiron's Avatar
kdoiron kdoiron is offline Offline
Light Poster

Re: How to compare two files and save difference in thrid file

  #3  
May 20th, 2008
Of course, you'll have to do it the other way too - to look through file2 to be sure everything there is also in file1.
But I don't like SPAM!
Reply With Quote  
Join Date: Dec 2006
Posts: 443
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 62
woooee woooee is offline Offline
Posting Pro in Training

Re: How to compare two files and save difference in thrid file

  #4  
May 20th, 2008
We had a topic like this a short while ago. The easiest/most efficient method is to use two sets and compare with set1.difference(set_2) and vice-versa. Also, python has difflib which may or may not be more than you want. http://docs.python.org/lib/differ-examples.html
Reply With Quote  
Join Date: Apr 2008
Posts: 4
Reputation: sreelatha is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sreelatha sreelatha is offline Offline
Newbie Poster

Re: How to compare two files and save difference in thrid file

  #5  
May 29th, 2008
Hi guys,

Thanks for your reply. It helped me a lot
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 8:50 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC