2

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 ()
1
  • And what output you get ? Commented May 6, 2016 at 5:39

3 Answers 3

3
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()
Sign up to request clarification or add additional context in comments.

1 Comment

Keep in mind, OP, that this is very inefficient, especially for large files.
2

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...]

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.