I am working on a code that compares two text files in python and prints the differences between the two. I was told to use sets. Is it also possible to have a dialogue box to choose the file, instead of manually inputting file names? I am very beginner level at python, so if you could write out the code, I would really appreciate it.
File1.txt
hamburgers
potatoes
avocado
grapes
seaweed
File2.txt
cheeseburgers
potatoes
peanuts
grapes
seaweed
so I would want the code to print cheeseburgers, peanuts
This is what I have but not sure if it's right:
old_path = 'File1.txt'
new_path = 'File2.txt'
old_lines = file(old_path).read().split('\n')
new_lines = file(new_path).read().split('\n')
old_lines_set = set(old_lines)
new_lines_set = set(new_lines)
old_added = old_lines_set - new_lines_set
old_removed = new_line_set - old_lines_set
for line in old_lines:
if line in old_added:
print '-' , line.strip()
elif line in old_removed:
print '+' , line.strip()
for line in new_lines:
if line in old added:
print '-' , line.strip()
elif line in old_removed:
print '+' , line.strip ()