[Tutor] help on string replacement in a file

klappnase klappnase at freenet.de
Tue Aug 12 12:25:48 EDT 2003


On Tue, 12 Aug 2003 13:32:28 +0800
"kamariah lamim" <klhjhm at hotmail.com> wrote:

> Thank you for your code.
> may i know what i does it means by inplace?
> 

> >
> >I am not sure about that, but I think you should use the fileinput module 
> >for that, like:
> >
> >def modify5File():
> >     for line in fileinput.input(path-to-file, inplace=1):
> >         line = line.replace('+0.2000000000d+00', '0.678')
> >         sys.stdout.write(line)
> >
> >This should replace every '+0.2000000000d+00' in the file with '0.678' .
> >
> >Best regards
> >
> >Michael
> >
>
Hi,

the library reference says on this:

Optional in-place filtering: if the keyword argument inplace=1 is passed to input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place. If the keyword argument backup='.<some extension>' is also given, it specifies the extension for the backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read. 

I hope this helps

Best regards

Michael



More information about the Tutor mailing list