#!/usr/bin/python # -*- coding: GBK -*- #writer:xmnathan #py文件去注释 import re import os import ConfigParser Python='CleanNote' def ReadIni(path,section,option):#文件路径,章节,关键词 #读取ini cf=ConfigParser.ConfigParser() cf.read(path) value=cf.get(section,option)#如果用getint()则直接读取该数据类型为整数 return value def IsPassLine(strLine): #是否是可以忽略的行 #可忽略行的正则表达式列表 RegularExpressions=["""/'.*#.*/'""","""/".*#.*/"""", """/'/'/'.*#.*/'/'/'""","""/"/"/".*#.*/"/"/""""] for One in RegularExpressions: zz=re测试数据pile(One) if re.search(zz,strLine)==None: continue else: return True#有匹配 则忽略 return False def ReadFile(FileName): #读取并处理文件 fobj=open(FileName,'r') AllLines=fobj.readlines() fobj.close() NewStr='' LogStr='/n%20s/n'%(FileName.split('//')[-1])#输出的日志 nline=0 for eachiline in AllLines: index=eachline.find('#')#获取带注释句‘#'的位置索引 if index==-1 or nline >>End
ps:配置文件CleanNote.ini的格式
[CleanNote] SrcPath=E:/test DescPath=E:/test/newfiles
批量去除指定源文件夹中的py文件的注释,并生成拷贝与指定目的文件夹
希望本文所述对大家的Python程序设计有所帮助。
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did91300