您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Python文件去除注释的方法

2024/6/28 11:54:41发布33次查看
本文实例讲述了python文件去除注释的方法。分享给大家供大家参考。具体实现方法如下:
#!/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.compile(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<<<' os.system('pause')
ps:配置文件cleannote.ini的格式
[cleannote] srcpath=e:/test descpath=e:/test/newfiles
批量去除指定源文件夹中的py文件的注释,并生成拷贝与指定目的文件夹
希望本文所述对大家的python程序设计有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product