Reputation: 21
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 ()
Upvotes: 2
Views: 3591
Reputation: 410
Below Solution worked for me with my exact requirement.
f1=open((os.getcwd()) + "\\Test1.txt","r")
f2=open((os.getcwd()) + "\\Test2.txt","r")
for i, j in zip(f1, f2):
if i != j:
print(i.rstrip() + "\t" + j.rstrip())
f1.close()
f2.close()
Output:
R[10]=0x8 R[10]=0x13
R[54]=0x6 R[54]=0x4
R[59]=0x18 R[59]=0x58
R[60]=0x3d R[60]=0x4c
R[126]=0x59 R[126]=0xbd
Upvotes: 0
Reputation: 1325
An easier solution, using the built-in set features:
a = set(['hamburgers', 'potatoes', 'avocado', 'grapes', 'seaweed'])
b = set(['cheeseburgers', 'potatoes', 'peanuts', 'grapes', 'seaweed'])
a.difference(b)
b.difference(a)
The set.difference() function gives you set objects again, which you can process as you want. [I hope I am not solving homework problems for you...]
Upvotes: 2
Reputation: 348
doc = open(filename, 'r')
doc1 = open(filename, 'r')
f1 = [x for x in doc.readlines()]
f2 = [x for x in doc1.readlines()]
diff = [line for line in f1 if line not in f2] # lines present only in f1
diff1 = [line for line in f2 if line not in f1] # lines present only in f2
doc.close()
doc1.close()
Upvotes: 3