您的位置:首页 > 编程语言 > Python开发

Python 实现在对一个目录下所有文件,指定某一行之后添加内容(批处理脚本)

2013-01-03 15:48 1266 查看
#folder for output
outDir  = "D:\\ddd"
#After which line start to add new sentence
startAdd = 'Telnet WatchFor Fail "ERROR"'
#The keyword checked sentence we wanna add
ItemAdd  = ['Telnet WatchFor Fail "Error"','Telnet WatchFor Fail "error"']

def changeItems(path,pathout):
for filename in os.listdir(path):
if os.path.isdir(path + '\\' + filename):
if not os.path.exists(outDir + '\\' + filename):
os.mkdir(outDir + "\\" + filename)
changeItems(path + '\\' + filename,outDir + "\\" + filename )
else:
print "input:" , path + "\\" + filename
filein = open(path +"\\"+ filename, "rb+")
print "output:" , pathout +"\\"+ filename
fileout= open(pathout + "\\"+filename,"wb")
if(filename.split('.')[-1].lower() == "qar" ):
for line in filein.readlines():
if startAdd in line:
fileout.write(line+"\n")
for item in ItemAdd:
fileout.write(item +"\n"+"\n")
else:
fileout.write(line+"\n")
else:
bufout=filein.read()
fileout.write(bufout)
filein.close()
fileout.close()
import os
rootpath = os.path.abspath('.')
print rootpath
changeItems(rootpath,outDir)


有时候需要批处理对目录下的脚本的制定位置进行修改,可参考哈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐