[Tutor] Newbie: Help on comparing files
Isr Gish
isrgish at fusemail.com
Mon Jan 12 23:16:51 EST 2004
This is the code I wrote to compare the files.
def compare(oldfilepth, newfilepth, sep):
seplen = len(sep)
olddata = getfiledata(oldfilepth)
newdata = getfiledata(newfilepth)
fw = file('\\Temp\\Code Breaker\\only in old.txt', 'w')
index = 1
while index != -1:
index = olddata.find(sep)
if index != -1:
srchstr = olddata[:index]
olddata = olddata[index + seplen:]
else:
srchstr = olddata
srchlen = len(srchstr)
fnd_indx = newdata.find(srchstr)
if fnd_indx != -1: #Substring was found
newdata = newdata[:fnd_indx] + newdata[fnd_indx + srchlen + seplen:]
else: #The substring wasn't found
fw.write(srchstr + sep)
fw.close()
fw = file('\\temp\\code breaker\\only in new.txt', 'w')
fw.write(newdata)
fw.close()
def getfiledata(filepth):
fr = file(filepth)
data = fr.read()
fr.close()
return data
This is the basic code. (it still needs a few enhancements).
What I want to know is, if there is a quicker way. For example if I would first split the whole file into list if the find would be quicker.
Any other pointers about my code, would also be greatly appreciated.
Best Wishes
Isr Gish
More information about the Tutor
mailing list