[Tutor] Regex parsing and writing to file

Kent Johnson kent37 at tds.net
Tue Oct 16 19:57:26 CEST 2007


Peter Mexbacher wrote:

> I have the following task:
> 
> 1) read in a file line by line
> 2) parse each line with a regular expression, and substitute certain 
> patterns
> 3) end result: the old file with the substituted stuff (and of course 
> everything which did not need substitution)
> 
> Do I have to write each line out to a new file, and then rename, or can
> I substitute "in place" - that the changes are incorporated in the old
> file automatically?

If you use the fileinput module with inplace=1 it will take care of this 
for you and even make a backup file if you like.

Kent


More information about the Tutor mailing list