开发者

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

开发者 https://www.devze.com 2022-12-25 10:51 出处:网络
i would like to replace the lines function *{ by functio开发者_运维问答n *{echo \"The function starts here.\"

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

精彩评论

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

关注公众号