Skip to content

Instantly share code, notes, and snippets.

@13Cubed
Created January 27, 2016 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 13Cubed/e32b1c3070c443f6114e to your computer and use it in GitHub Desktop.
Save 13Cubed/e32b1c3070c443f6114e to your computer and use it in GitHub Desktop.
A simple file comparison utility written in Python.
#!/usr/bin/python
# audit-tool.py 2.0 - A simple file comparison utility.
# Copyright 2014 13Cubed. All rights reserved. Written by: Richard Davis
import sys
def compareFiles(filename1, filename2, ignorecase, bidirectional):
"""
Given two filenames and an ignorecase booelean, compares filename1
against filename2 and returns list of the differences and a count of
how many were found. If ignorecase is 1, the contents of both files
are read in as lowercase so that case differences are ignored. If
bidirectional is 1, filename1 is compared to filename2 and vice-versa.
"""
results = []
try:
f1 = open(filename1, 'rU')
except IOError:
print 'Could not find the specified file:', filename1
sys.exit(1)
try:
f2 = open(filename2, 'rU')
except IOError:
print 'Could not find the specified file:', filename2
sys.exit(1)
list1 = f1.readlines()
list2 = f2.readlines()
f2.close()
f1.close()
if ignorecase == 1:
for i in range(0,len(list1)):
list1[i] = list1[i].lower()
for i in range(0,len(list2)):
list2[i] = list2[i].lower()
diffs = set(list1) - set(list2)
if bidirectional == 1:
reverseDiffs = set(list2) - set(list1)
diffcount = 0
results.append('\n' + filename1 + ' -> ' + filename2 + ':\n')
for diff in diffs:
results.append(diff)
diffcount = diffcount + 1
if bidirectional == 1:
results.append('\n' + filename1 + ' <- ' + filename2 + ':\n')
for diff in reverseDiffs:
results.append(diff)
diffcount = diffcount + 1
return results, diffcount
def main():
if (len(sys.argv) < 3) or (len(sys.argv) > 5):
print 'usage: audit-tool.py filename1 filename2 [--ignorecase] [--bidirectional]'
sys.exit(1)
ignorecase = 0
bidirectional = 0
filename1 = sys.argv[1]
filename2 = sys.argv[2]
if len(sys.argv) == 4:
option1 = sys.argv[3]
if option1 == '--ignorecase':
ignorecase = 1
elif option1 == '--bidirectional':
bidirectional = 1
else:
print 'unknown option: ' + option1
sys.exit(1)
elif len(sys.argv) == 5:
option1 = sys.argv[3]
option2 = sys.argv[4]
if option1 == '--ignorecase':
ignorecase = 1
elif option1 == '--bidirectional':
bidirectional = 1
else:
print 'unknown option: ' + option1
sys.exit(1)
if option2 == '--ignorecase':
ignorecase = 1
elif option2 == '--bidirectional':
bidirectional = 1
else:
print 'unknown option: ' + option2
sys.exit(1)
(results, diffcount) = compareFiles(filename1, filename2, ignorecase, bidirectional)
if diffcount:
print '\n%d difference(s) found.' % (diffcount)
for line in results:
print line,
else:
print '\nNo differences -- files are identical.'
print '\nCopyright (C) 2014 13Cubed. All rights reserved.'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment