, если все ваши сценарии "хорошо закодированы",
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