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
  Produce One file Per PurchaseOrder jland47 1 276 Jan-26-2024, 11:38 AM
Last Post: Larz60+
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 2,878 Jun-29-2023, 11:57 AM
Last Post: gologica
Question Need help for a python script to extract information from a list of files lephunghien 6 988 Jun-12-2023, 05:40 PM
Last Post: snippsat
  script to calculate data in csv-files ledgreve 0 1,019 May-19-2023, 07:24 AM
Last Post: ledgreve
  [split] How to resolve version conflicts in Python? atonalwilson 1 916 May-04-2023, 09:02 AM
Last Post: buran
  How to resolve version conflicts in Python? taeefnajib 0 860 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  list the files using query in python arjunaram 0 641 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  python sql query single quote in a string mg24 1 974 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  "SUMIF" type query in Python (help required) BlainEillimatta 0 790 Oct-06-2022, 09:08 AM
Last Post: BlainEillimatta
  How to resolve my problem in Pycharm? bshoushtarian 0 809 Sep-26-2022, 11:45 AM
Last Post: bshoushtarian

Forum Jump:

User Panel Messages

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