Python Forum
Diff Check - Print from dcmp.diff_files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Diff Check - Print from dcmp.diff_files
#1
Hi All,

I received my first ever python scripting task today and I need some assistance. I am normally an Appian developer.

I have two directories with 97 *.xml files in each directory (root1 and root2 in the example below). The 97 files are named the same in both root1 and root2, but each set comes from a different environment.

I need to iterate through each of the 97 files in root1 directory, locate the file with the same name in root2, do a comparison to determine which files have differences between the two directories, and finally print the line/s of code from both root1 and root2 versions of the file where there are differences.

I have worked out something where I am able to print the names of files shared between root1 and root2 where code differences exist, but not the specific line/s of code.

The python script below is where I'm at with this so far. It generates no errors, but it also doesn't print anything. I'm able to confirm that all 97 xml files have differences between root1 and root2 versions.

Any help with this is very much appreciated. Thanks!


from filecmp import dircmp
from difflib import Differ
			
root1 = 'AppsToCheck/Example - INT/'
root2 = 'AppsToCheck/Example - TEST2/'

def print_diff_files(dcmp):
	for name in dcmp.diff_files:
		with open(root1 + str(name)) as f1:
			with open(root2 + str(name)) as f2:
				differ = Differ() 
				for line in differ.compare(f1.readlines(), f2.readlines()): 
					if line.startswith(" "): 
						print(line[2:], end="")

dcmp = dircmp(root1, root2) 
print_diff_files(dcmp)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to diff pandas rows and modify column value Mekala 1 1,654 Sep-18-2020, 12:38 PM
Last Post: Mekala
  Diff. between Py 2.7 and 3 ebolisa 2 7,864 Dec-15-2018, 11:46 AM
Last Post: ebolisa
  Diff. between Py 2.7 and 3 ebolisa 1 2,083 Nov-01-2018, 06:45 PM
Last Post: wavic
  Diff between 1 file to serveral files tcpip 3 3,806 Aug-21-2017, 08:54 AM
Last Post: tcpip

Forum Jump:

User Panel Messages

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