[Tutor] Search and Replace

Roeland Rengelink r.b.rigilink@chello.nl
Thu, 28 Jun 2001 09:22:58 +0200


VanL wrote:
> 
> Hello,  tahks for the comments.
> 
> Roeland Rengelink wrote:
> 
> >
> >Why use regular expressions, when there is a perfectly good
> >string.replace() function that does exactly what you want?
> >
> Because using regexes makes it a bit more general, and because (in this
> case) I needed to match a regex.
> I am thinking of rewriting and expanding this to include a mode
> argument, with modes 0,1,2 = string replacement, regex, compiled regex
> object.
> 

I see now, sounds good.

<snip>
> 
> This, once again, truncates the file and then writes it out.  I am
> worried about possible data loss;  Ideally, I would
> open the file for reading and writing, read in one line, change it,
> write it back out, read another line, etc.  That way, if the script was
> stopped halfway through execution, the replacement wouldn't be finished,
> but the file contents would not be lost.
> 

How about, after reading the file, renaming it to something like
fname.backup
(renaming could be made reasonably save on most OSs), and only if this
is succesfull
writing the new file ?

Roeland

-- 
r.b.rigilink@chello.nl

"Half of what I say is nonsense. Unfortunately I don't know which half"