-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathunique-file-lines.py
executable file
·51 lines (36 loc) · 1.22 KB
/
unique-file-lines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
# boolean comparasion of two text files
# - lines exist in first file and not exist in second
# - lines exist in both files
# - lines exist not exist in first file but exist in second
def read_file_lines(path_to_file):
with open(path_to_file) as f:
return [each_line.strip() for each_line in f.readlines()]
def print_list(lines):
for each_line in lines:
print(each_line)
def diff_left(list1, list2):
return [each_line for each_line in list1 if each_line not in list2]
def diff_right(list1, list2):
return [each_line for each_line in list2 if each_line not in list1]
def diff_eq(list1, list2):
return [each_line for each_line in list2 if each_line in list1]
if __name__=='__main__':
if len(sys.argv) <= 3:
print(sys.argv)
print(">>> 'first_file' and [left,right,equals] and 'second_file' files are mandatory")
sys.exit(1)
left = read_file_lines(sys.argv[1])
if sys.argv[2] == "equals":
operation = diff_eq
elif sys.argv[2] == "left":
operation = diff_left
elif sys.argv[2] == "right":
operation = diff_right
else:
None
if not operation:
print("second operation should be one of [left, right, equals]")
sys.exit(2)
right = read_file_lines(sys.argv[3])
print_list(operation(left, right))