Tuesday, 13 August 2013

is this python file.seek() routine correct?

is this python file.seek() routine correct?

This routine looks ok to me but ends up writing rubbish to the file.
lines_of_interest is a set of lines (896227L, 425200L, 640221L ect) that
need to be changed in the file. The if else routine determines what is
changed on that line. This is the first time I have used seek() but
believe the syntax is correct. Can anyone spot any errors in the code that
will get it working correctly?
outfile = open(OversightFile, 'r+')
for lines in lines_of_interest:
for change_this in outfile:
line = change_this.decode('utf8', 'replace')
outfile.seek(lines)
if replacevalue in line:
line = line.replace(replacevalue, addValue)
outfile.write(line.encode('utf8', 'replace'))
break#Only check 1 line
elif not addValue in line:
#line.extend(('_w\t1\t'))
line = line.replace("\t\n", addValue+"\n")
outfile.write(line.encode('utf8', 'replace'))
break#Only check 1 line
outfile.close()

No comments:

Post a Comment