Python Forum
Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu
#1
I wanted to write a python script that gives me the difference between two txt files that contain list of domains.

Below is my script that I was able to create.

import difflib

file1 = open("2.txt").readlines()
file2 = open("diff.txt").readlines()
with open ('diff.txt', 'w', encoding = 'UTF8') as diff:
	for line in difflib.unified_diff(file2,file1,n=0):
		for prefix in ('---','+++','@@'):
			if line.startswith(prefix):
				break
		if line.startswith('+'):
			diff.write("New DNS Entry:\n{}\n".format(line[1:]))
		elif line.startswith('-'):
			diff.write("Removed DNS Entry:\n{}\n".format(line[1:]))
Since unified_diff produces context and characters like +++, --- & @@, I have modified my code to omit this and disabled context.

The output I receive from the above code is as follows:
Output:
Removed DNS Entry: -- New DNS Entry: ++ Removed DNS Entry: example.com
As seen above, the output titles anything with a - as "Removed DNS Entry" and anything with a + as "New DNS Entry" and stops at very first iteration of a line change between two txt files (although there are multiple changes beyond example.com)

The output I'm expecting is that any line that starts with a + should be grouped together and titled "New DNS Entries" and similarly any line that starts with - should be grouped together and titled "Removed DNS Entries"

Eventually, the group of "New DNS Entries" should display their appropriate DNS resolutions.

Something like this..

Output:
New DNS Entries: example1.com example2.com Removed DNS Entries: example13.com example21.com DNS Resolution for new entries: example1.com CNAME example.aws.com example2.com A 192.168.1.1
Any help here is much appreciated!
Reply
#2
If difflib does not work for you,
then do it the old way:

Read file1 into memory and sort the lines.
Read file2 into memory and sort the lines.
Compare line by line.
Reply
#3
(May-22-2019, 03:42 AM)heiner55 Wrote: Read file1 into memory and sort the lines.
Read file2 into memory and sort the lines.
Compare line by line.

actually loading each file into set and using set operations would be simpler
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to find difference between fake driving license or real using python? pjaymn 2 188 Dec-20-2020, 08:41 PM
Last Post: jefsummers
  How to resolve numpy ValueError: dtype.descr Py_veeran 0 274 Aug-18-2020, 06:46 PM
Last Post: Py_veeran
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 850 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Project files, Python files Heyjoe 4 531 Jun-27-2020, 07:30 PM
Last Post: Heyjoe
  How to kill a bash script running as root from a python script? jc_lafleur 4 675 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 501 May-28-2020, 05:27 PM
Last Post: micseydel
  python 3 find difference between 2 files pd007 2 456 May-22-2020, 01:16 AM
Last Post: Larz60+
  Need help to resolve this MK2020 1 691 Mar-28-2020, 05:04 AM
Last Post: SheeppOSU
  Python mysql query help please tduckman 4 821 Mar-13-2020, 03:42 PM
Last Post: Marbelous
  Difference Between 2 files enigma619 3 625 Dec-21-2019, 01:39 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020