[Tutor] Working with files

Alan Gauld ukc802591034 at btconnect.com
Sat Aug 27 00:01:55 CEST 2005


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

I didn't miss it but I did misread it! :-)

>    file.write(contents[pos + len(name):])

I assumed 'name' was here referring to the actual value inserted.
But of course that would have been a different bug to the one I
described.

> 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.

Yes indeed, now that I actually see what's happening it's
quite cute!

> It also avoids making a new string with the modified data.

True but it does involve a new string fom the slice, so I wouldn't
expect a huge saving there. But the random file access should be
faster than a full rewrite operation.

Thanks for pointing out my mistake and my compliments to the OP!

Alan G.



More information about the Tutor mailing list