•
•
•
•
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
![]() |
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
Simple read the use file.readlines() on both files to give you 2 arrays
so something like this
something along those lines
Chris
so something like this
Python Syntax (Toggle Plain Text)
f1 = open("file1.txt", "r") f2 = open("file2.txt", "r") fileOne = f1.readlines() fileTwo = f2.readlines() f1.close() f2.close() outFile = open("results.txt", "w") x = 0 for i in fileOne: if i != fileTwo[x]: outFile.write(i+" <> "+fileTwo[x]) x += 1 outFile.close()
something along those lines
Chris
Last edited by Freaky_Chris : May 19th, 2008 at 12:51 pm.
•
•
Join Date: Dec 2006
Posts: 443
Reputation:
Rep Power: 2
Solved Threads: 62
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Python Forum
- Previous Thread: Installing Pygame
- Next Thread: C++ ObjectWrapper for Python


Linear Mode