Reputation: 29
I have been trying to compare two text files in Python, essentially I want to open them and compare a single character at a time if the characters are different add 1 to a counter and then display this value.
Here is what I have so far:
#!/usr/bin/env python
diff = 0
import random
import string
import sys
file_A = sys.argv[1]
file_B = sys.argv[2]
read_A=open(file_A,'r').read()
read_B=open(file_B,'r').read()
for a in read_A:
for b in read_B:
if a != b:
diff = diff + 1
return diff
break
print "No of Differences ",diff
Right now it does count but the value it returns does not seem correct.
Upvotes: 0
Views: 5995
Reputation: 639
Looping isn't certainly the pythonic way to do it. Without using any external library, you can use simple list comprehension to find the instances of differences between two files:
If the order is significant:
x=[i for i, j in zip(read_A, read_B) if i != j]
print len(x)
If the order is not significant:
x=[i in read_B for i in read_A]
#Now count the instances where the comparison resulted in False
i = x.count(False)
Upvotes: 1
Reputation: 19352
If you have two strings which you want to compare character by character, it seems best to zip them.
So, once you've doe this:
read_A=open(file_A,'r').read()
read_B=open(file_B,'r').read()
you do:
for char_a, char_b in zip(read_A, read_B):
if char_a != char_b:
# something
If your files are very big, you may wish to only read character by character and use izip
instead of zip
...
BTW...
You are not closing your files. It is a good idea to always access files this way:
with open(file_A, 'r') as f1:
read_A = f1.read()
with open(file_B, 'r') as f2:
read_B = f2.read()
Upvotes: 1
Reputation: 1768
The simplest way to do this is to use difflib. It doesn't require any additional eggs for python and gives really good results for tracking various kinds of diff checks. The result of a diff check would be what you'd look toward to check for equality as well as close matches. That can be important as well if you allow for weighting of results.
Upvotes: 2