最近在用python自动生成c++的类.因为这些类会根据需求不同产生不同的类,所以需要用python自动生成.由于会产生大量的类,而且这些类是变化的.所以如果是在某个.h中要用include来加载这些类,会累死人的.所以用python来生成这些类的头文件引用和类的类名声明
先看例子,再聊python的读写文件的代码 在聊聊我的python代码
------------------------>
好吧.上面的图就是面临的需求
下面来聊聊从网上找的读写文件的python代码吧.csdn的一个博主写的很详细. python如何读写文件:http://blog.csdn.net/adupt/article/details/4435615
仔细阅读完博主的讲解后,有这么一段
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
针对上面的这段代码我会在来个说明 关于 try except finally with 等的帖子 <python中的 try except finally with>
恩.好了,下面把我的python代码也就是实现最开始的截图的代码放这里 供自己以后类似的情况来参考
#!/usr/bin/python #此文件是自动生成头文件声明的python脚本
# -*- encoding: utf-8 -*-
import os def generateRecordsHeaderInclude(folderPath): try:
filePath = os.path.join(folderPath,"misc_records.h")
with open(filePath, "w") as file:
recordsFolderPath = os.path.join(folderPath, 'records') #E:\fare_uuid\misc\include\misc\records 存放生成的类的.h
includeNames = os.listdir(recordsFolderPath) #获取records文件夹下的所有文件名
#写c++代码
file.write("#ifndef MISC_MISC_RECORDS_H_\n")
file.write("#define MISC_MISC_RECORDS_H_\n")
file.write("\n//Records\n") for includeName in includeNames:
file.write('#include "misc/records/%s"\n' % includeName) file.write("#endif /* MISC_MISC_RECORDS_H_ */")
except:
print "create file %s\\misc_records.h error" % folderPath
return print "create file %s\\misc_records.h success!" % folderPath if __name__ == '__main__':
homedir = r"E:\farestar_uuid\misc"
folderPath = os.path.join(homedir, 'include', 'misc')
print 'folderPath = ' + folderPath generateRecordsHeaderInclude(folderPath) #E:\farestar_uuid\misc\include\misc
#!/usr/bin/python #此文件是自动生成c++类的类名声明的python脚本
# -*- encoding: utf-8 -*-
import os def getStructName(recFilePath, fwdFilePath):
try:
with open(recFilePath, 'r') as file:
for line in file:
if 'struct' in line and ': public MiscObject' in line:
words = line.split(" ")
structIndex = words.index('struct') # 类名里面是 struct xxx : public MiscObject 所以读到这一行的时候就拆解处 类名来
return words[structIndex+1]
except:
print 'read %s error' % filePath
print 'create %s error' % fwdFilePath
raise RuntimeError("create %s error in genRecordsStructDeclare.py" % fwdFilePath) def generateRecordsStructDeclare(folderPath):
try:
fwdFilePath = os.path.join(folderPath,"misc_fwd.h") #类名声明的头文件
with open(fwdFilePath, "w") as file:
recordsFolderPath = os.path.join(folderPath, 'records') #所以的类名的.h文件在records文件夹下
recFileNames = os.listdir(recordsFolderPath) file.write("#ifndef MISC_MISC_FWD_H_\n")
file.write("#define MISC_MISC_FWD_H_\n") file.write('#include "misc/miscid.h"\n')
file.write('#include "misc/miscerrorcode.h"\n')
file.write('namespace misc\n')
file.write('{\n')
for recFileName in recFileNames:
recFilePath = os.path.join(recordsFolderPath, recFileName) #xxxx/recors/agency.h
structName = getStructName( recFilePath, fwdFilePath)
file.write(' struct %s;\n' % structName) #生成一行类名声明
file.write('}\n') #file.write('#include "misc/records/%s"\n' % includeName) file.write("#endif /* MISC_MISC_FWD_H_ */")
except:
print "create file %s error" % fwdFilePath
return print "create file %s success!" % fwdFilePath if __name__ == '__main__':
homedir = r"E:\farestar_uuid\misc"
folderPath = os.path.join(homedir, 'include', 'misc')
print 'folderPath = ' + folderPath generateRecordsHeaderInclude(folderPath)