Python Forum
Diff Check - Print from dcmp.diff_files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Diff Check - Print from dcmp.diff_files (/thread-11110.html)



Diff Check - Print from dcmp.diff_files - ipuente - Jun-22-2018

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)