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
.
精彩评论