[Tutor] overwriting input file

Bala subramanian bala.biophysics at gmail.com
Wed Feb 25 13:44:04 CET 2009


Hello all,

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.

pat1=" R"
pat2="U5"
pat3="A3"
from sys import argv
files=argv[1:]
for names in files:
        out=open(names + '_new','w')  # Here i creat new files to write the
content which i dnt want
        for line in open(names):
                if pat1 in line:
                        line=line.replace(pat1,"  ")
                if pat2 in line:
                        line=line.replace(pat2,"U ")
                if pat3 in line:
                        line=line.replace(pat3,"A ")
                out.write(line)

query 2) How should I use wild cards to open files in python. Say I have
files with names *.dat  in a directory, i want the program to open every
file with extension .dat and do the process.

Thanks in advance,
Bala
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090225/828b21bc/attachment.htm>


More information about the Tutor mailing list