[Tutor] overwriting input file

Alan Gauld alan.gauld at btinternet.com
Wed Feb 25 19:53:49 CET 2009

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

For copying and renaming filers see the os and shutil modules
and the Using the OS topic in my tutorial.

> query 2) How should I use wild cards to open files in python.

Look at the glob module. It uses wildcards to build lists of
matching file names.


Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list