开发者

Python 2.5.2 script that add "The function starts here" to all the functions of the files of a directory

i would like to replace the lines

function *{

by

functio开发者_运维问答n *{echo "The function starts here."

where * is what ever.

Any idea how to do that in Python?

Regards

Javi


re.compile(r'(^function .*{)', re.M).sub(r'\1echo "The function starts here."', s)


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜