这里主要使用正则表达式查找 注释的内容,把它替换为空白。
匹配注释的正则表达式
C_Rule = "(///*(/s|.)*?/*//)|(////.*)"
以下是代码:
- #--*-- coding: cp936 --*--
- import glob
- import os
- import re
- C_Rule = "(///*(/s|.)*?/*//)|(////.*)"
- file_type_list = ["h", "c", "cc", "cpp", "H", "C", "cc", "CPP"]
- pS = re.compile(C_Rule) ;
- def updatefile(path):
- ''''' 把文件读进内存里,用正则表达式查找
- '''
- print path ;
- fw = open(path, "rw") ;
- str = fw.read() ;
- fw.close() ;
- #mS = pS.match(str) ;
- #mS.
- str = re.sub(C_Rule, "", str) ;
- print "connet : %s " % str ;
- fw = open(path, "w") ;
- fw.write(str)
- def listfiles(path, file_types):
- '''''枚举path目录下的所有符合要求的文件
- '''
- for file in os.listdir(path):
- listpath = path + "//"+file ;
- if os.path.isdir(listpath):
- listfiles(listpath, file_types);
- elif os.path.isfile(listpath):
- splitlist = listpath.split('.')
- m = len(splitlist)
- prefx = splitlist[m-1]
- #print prefx
- if prefx in file_types:
- updatefile(listpath) ;
- def remove_comment(path, file_types):
- listfiles(path, file_types)
- if __name__ == '__main__':
- path = "."
- remove_comment(path, file_type_list) ;
- #system("pause")