[Tutor] Working with files

Kent Johnson kent37 at tds.net
Fri Aug 26 15:28:16 CEST 2005

Alan G wrote:
> ARe you sure? The problem using seek and write is that if the data
> you are inserting is bigger than your marker you will overwrite the
> data following the marker.

Alan, you missed the last part of the code - he writes the rest of the data following the match into the file.

This is an innovative approach which may have some benefit over the usual / read the whole file / change the data in memory / write the whole file / method as it avoids re-writing the part of the file before the change. It also avoids making a new string with the modified data. These could be significant benefits if the data is very large.

> This does not insert it overwrites.
> You are better to insert the content into contents and
> then just write it all back to the file in one move.
>>   file.write(contents[pos + len(name):])
>>   file.close()

This is the piece you missed. ^^^^


More information about the Tutor mailing list