[Tutor] overwriting input file

Kent Johnson kent37 at tds.net
Wed Feb 25 20:33:17 CET 2009


On Wed, Feb 25, 2009 at 1:53 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "Bala subramanian" <bala.biophysics at gmail.com> wrote
>
>> query 1) How should i overwrite the input file
>> I want to open 5 files one by one, do some operation on the lines and
>> write
>> the modified lines on the same file (overwritting). Can some please tell
>> me
>> how to do it.
>
> You don't really want to do that! Too risky.
> Better to do what you are doing and then when it has all worked
> and you close both input and output files you can delete the
> original and rename the outfile to the original input file name.
> In fact to be really safe don't delete the old file but rename
> it to .bak...

The fileinput module with inplace=1, backup='.bak' does close to what
you are describing. It renames the input file before opening it, so if
there is a failure it will be renamed.

Kent


More information about the Tutor mailing list