Skip to content

Instantly share code, notes, and snippets.

@mkows
Created January 26, 2017 15:19
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 mkows/984f7ce3dabacc0c1c066a9214be45c3 to your computer and use it in GitHub Desktop.
Save mkows/984f7ce3dabacc0c1c066a9214be45c3 to your computer and use it in GitHub Desktop.
import datetime
import time
# Find out lines occurring in both "file_a" and "file_b" and print to "output-<ts>.txt"
def build_timestamp():
t = time.time()
return datetime.datetime.fromtimestamp(t).strftime('%Y%m%d-%H%M%S')
# params
file_a_path = 'file_a.txt'
file_b_path = 'file_b.txt'
output_file_path = 'output-' + build_timestamp() + '.txt'
file_a = open(file_a_path, 'r')
file_b = open(file_b_path, 'r')
output_file = open(output_file_path, 'w')
file_a_entries = file_a.read().splitlines()
file_a.close()
file_b_entries = file_b.read().splitlines()
file_b.close()
size_a = len(file_a_entries)
size_b = len(file_b_entries)
print str(size_a) + ' <- file a size'
print str(size_b) + ' <- file b size'
for a in file_a_entries:
if a in file_b_entries:
output_file.write(a + '\n')
print 'file with common lines: ' + output_file_path
output_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment