Skip to content

Instantly share code, notes, and snippets.

@manasmbellani
Created February 27, 2019 12:46
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 manasmbellani/d747a8e67bb4371bd0a216842fb94cbe to your computer and use it in GitHub Desktop.
Save manasmbellani/d747a8e67bb4371bd0a216842fb94cbe to your computer and use it in GitHub Desktop.
compare-lists.py - Compares 2 lists aka items that appear in one list but do not show up in the second list
#!/usr/bin/env python3
import os
import sys
import argparse
parser = argparse.ArgumentParser(description="Compare 2 lists - Finds items in list1, not present in list2")
parser.add_argument("-l1", "--list1", dest="list1", action="store", required=True,
help="list1")
parser.add_argument("-l2", "--list2", dest="list2", action="store", required=True,
help="list2")
parser.add_argument("-c", "--case-sensitive-search", dest="case_sensitive_search", action="store_true",
help="Case sensitive search")
args = parser.parse_args()
list1 = args.list1
list2 = args.list2
case_sensitive_search = args.case_sensitive_search
if not os.path.isfile(list1):
print("[-] list1: {} not found".format(list1))
sys.exit(1)
if not os.path.isfile(list2):
print("[-] list2: {} not found".format(list2))
sys.exit(1)
if not case_sensitive_search:
with open(list2, "r+") as f:
list2_contents = f.read()
with open(list1, "r+") as f:
with open(list2, "r+") as f2:
for line1 in f:
line1_nws = line1.strip()
line1_found_in_file2 = False
for line2 in f2:
line2_nws = line2.strip()
if line2_nws == line1_nws:
line1_found_in_file2 = True
break
if not line1_found_in_file2:
print(line1_nws)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment