Bala subramanian wrote:
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.

import fileinput
pat1=" R"
pat2="U5"
pat3="A3"
files = fileinput.Fileinput(sys.argv[1:], inplace = 1)
for i in files:
  if pat1 in i: print i.replace(pat1," ")
  if pat2 in i: print i.replace(pat2,"U ")
  if pat3 in i: print i.replace(pat3,"A ")
files.close()

should work for you. Check out the fileinput documentation for details especially the 'inplace' argument. It's untested though.

Personally speaking, I think the stdout redirection business violates the 'explicit is better than implicit' rule.


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.

import glob
glob.glob(r"*.dat")

should do that for you.

--
~noufal
http://nibrahim.net.in/
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to