tplFile = open(tplFilePath)
gFile = open(path+filename ,"w")
#method 1 with Template and substitute(safe_substitute)
lines=[]
tmp=Template(tplFile.read())
lines.append(tmp.substitute(
author = author,
now = now,
testObject = testObj,
testObjVarName = testObjVarName,
version = version))
gFile.writelines(lines)
'''
#Method 2, with replace
fileList = tplFile.readlines()
for fileLine in fileList:
line = fileLine.replace('${author}',author)\
.replace('${now}',now)\
.replace('${testObject}',testObj)\
.replace('${version}',version)\
.replace('${testObjVarName}',testObjVarName)
print line
gFile.writelines(line)
'''
tplFile.close()
gFile.close()
print 'generate %s over. ~ ~' % (path+filename)
运行结果
generate D:\Project\Python\code_gen\MBIM_Service_Basic_connect_device.java over. ~ ~
generate D:\Project\Python\code_gen\MBIM_Service_Sms_device.java over. ~ ~
generate D:\Project\Python\code_gen\MBIM_Service_Phonebook_device.java over. ~ ~
《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版]
《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码]
在Ubuntu下用Python搭建桌面算法交易研究环境