Best Way To Check New-line-independent-identity Of 2 Files With Python
Solution 1:
I think a simple convenience function like this should do the job:
from itertools import izip
defareFilesIdentical(filename1, filename2):
withopen(filename1, "rtU") as a:
withopen(filename2, "rtU") as b:
# Note that "all" and "izip" are lazy# (will stop at the first line that's not identical)returnall(myprint() and lineA == lineB
for lineA, lineB in izip(a.xreadlines(), b.xreadlines()))
Solution 2:
Try the difflib
module - it provides classes and functions for comparing sequences.
For your needs, the difflib.Differ
class looks interesting.
class difflib.Differ
This is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses SequenceMatcher both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines.
See the differ example, that compares two texts. The sequences being compared can also be obtained from the readlines()
method of file-like objects.
Solution 3:
Looks like you just need to check if files are same or not ignoring whitespace/newlines.
You can use a function like this
defdo_cmp(f1, f2):
bufsize = 8*1024
fp1 = open(f1, 'rb')
fp2 = open(f2, 'rb')
whileTrue:
b1 = fp1.read(bufsize)
b2 = fp2.read(bufsize)
ifnot is_same(b1, b2):
returnFalseifnot b1:
returnTruedefis_same(text1, text2):
return text1.replace("\n","") == text2.replace("\n","")
you can improve is_same
so that it matches according to your requirements e.g. you may ignore case too.
Post a Comment for "Best Way To Check New-line-independent-identity Of 2 Files With Python"