Skip to content

Instantly share code, notes, and snippets.

@AStevensTaylor
Last active December 20, 2015 10:09
Show Gist options
  • Save AStevensTaylor/6113056 to your computer and use it in GitHub Desktop.
Save AStevensTaylor/6113056 to your computer and use it in GitHub Desktop.
Finds differences in two directories, attempts to read files, and prints the differences in those files; disregarding position in the file.
#! /usr/bin/python
import os, os.path, sys
def getFiles(dir):
os.chdir(os.path.join(os.getcwd(), dir))
file_list = []
for root, _, filenames in os.walk('.'):
for filename in filenames:
file_list.append(os.path.join(root, filename))
os.chdir('..')
return file_list
dir1= sys.argv[1]
dir2= sys.argv[2]
list1= getFiles(dir1)
list2= getFiles(dir2)
intersection = [i for i in list1 if i in list2]
list1Exclusive = [i for i in list1 if i not in list2]
list2Exclusive = [i for i in list2 if i not in list1]
print('{}: {}, Intersection: {}, {}: {}'.format(os.path.basename(dir1) ,len(list1Exclusive), len(intersection),os.path.basename(dir2), len(list2Exclusive)))
exclusiveLines1 = []
exclusiveLines2 = []
nonReadbales = []
for file in intersection:
path1 = os.path.join(dir1, file[2:])
path2 = os.path.join(dir2, file[2:])
try:
lines1 = open(path1).read().splitlines()
lines2 = open(path2).read().splitlines()
exclusiveLines1.extend([(path1, lines1.index(i) + 1) for i in lines1 if i not in lines2])
exclusiveLines2.extend([(path2, lines2.index(i) + 1) for i in lines2 if i not in lines1])
except UnicodeDecodeError:
nonReadbales.append(path1)
print('{} Exclusive Files'.format(os.path.basename(dir1)))
for i in set(list1Exclusive):
print(i)
print('{} Exclusive Files'.format(os.path.basename(dir2)))
for i in set(list2Exclusive):
print(i)
print('{} Exclusive Lines'.format(os.path.basename(dir1)))
for i in set(exclusiveLines1):
print(i)
print('{} Exclusive Lines'.format(os.path.basename(dir2)))
print('==*==*==*==*==*==*==*==')
for i in set(exclusiveLines2):
print(i)
print('==*==*==*==*== NON-READABLES ==*==*==*==*==')
for i in set(nonReadbales):
print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment