tags:

views:

59

answers:

3

Hi,

i would like to replace the lines

function *{

by

function *{echo "The function starts here."

where * is what ever.

Any idea how to do that in Python?

Regards

Javi

A: 

Apply a regular expression replace to the text. The module you are looking for is re.

ndim
+3  A: 
re.compile(r'(^function .*{)', re.M).sub(r'\1echo "The function starts here."', s)
SilentGhost
+1 for remembering to use `re.M`.
Mark Byers
+1  A: 

if all your scripts are "well coded",

import fileinput,os
root="/path"
path=os.path.join(root,"mydir")
os.chdir(path)
for file in os.listdir("."):
    if os.path.isfile(file) and file.endswith(".txt"): # do for txt files
        for line in fileinput.FileInput(file,inplace=1):
            line=line.rstrip()
            if "function" in line and "{" in line:                     
                 s=line.split("{")
                 s.insert(1,'{echo "The function starts here."')
                 line=' '.join(s)
            print line
ghostdog74
did you test your code?
SilentGhost