国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python文件去除注釋的方法

2020-02-23 01:23:12
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python文件去除注釋的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

#!/usr/bin/python # -*- coding: GBK -*- #writer:xmnathan #py文件去注釋 import re import os import ConfigParser Python='CleanNote' def ReadIni(path,section,option):#文件路徑,章節(jié),關(guān)鍵詞   #讀取ini  cf=ConfigParser.ConfigParser()   cf.read(path)   value=cf.get(section,option)#如果用getint()則直接讀取該數(shù)據(jù)類型為整數(shù)   return value def IsPassLine(strLine):   #是否是可以忽略的行   #可忽略行的正則表達(dá)式列表   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<3 or IsPassLine(eachline):       if eachiline.strip()!='':#排除純空的行         NewStr=NewStr+eachiline     else:       if index!=0:         NewStr=NewStr+eachiline[:index]+'/n'#截取后面的注釋部分         LogStr+="ChangeLine: %s/t%s"%(nline,eachline[index:])     nline+=1   return NewStr,LogStr def MakeCleanFile(SrcPath,DescPath,FileList):   fLog=open(DescPath+'//'+'CleanNoteLog.txt','w')   for File in FileList:     curStr,LogStr=ReadFile(SrcPath+'//'+File)     fNew=open(DescPath+'//'+File,'w')     fNew=write(curStr)     fNew.close()     fLog.write(LogStr)   fLog.close() def Main():   #從ini獲取源文件夾及目標(biāo)文件夾路徑   IniPath=os.getcwd()+'//'+PtName+'.ini'   SrcPath=ReadIni(IniPath,PyName,'SrcPath')#源文件夾   DescPath=ReadIni(IniPath,PyName,'DescPath')#目的文件夾   #如果目的文件夾不存在,創(chuàng)建之   if not os.path.exists(DescPath):     os.makedirs(DescPath)   FileList=[]   for files in os.walk(SrcPath):     for FileName in files[2]:       if FileName.split('.')[-1]=='py':         FileList.append(FileName)   MakeCleanFile(SrcPath,DescPath,FileList) if __name__=='__main__':   Main()   print '>>>End<<<'   os.system('pause') 

ps:配置文件CleanNote.ini的格式

[CleanNote] SrcPath=E:/test DescPath=E:/test/newfiles 

 
批量去除指定源文件夾中的py文件的注釋,并生成拷貝與指定目的文件夾

希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南岸区| 蒲江县| 盐源县| 庆云县| 万源市| 锡林郭勒盟| 当阳市| 广汉市| 靖边县| 富民县| 定陶县| 丰台区| 叶城县| 饶河县| 泰安市| 当阳市| 宣城市| 七台河市| 德州市| 稻城县| 雅安市| 寿阳县| 图木舒克市| 渭源县| 宝鸡市| 桃江县| 仲巴县| 莱阳市| 南江县| 京山县| 镇赉县| 洞头县| 兰考县| 云霄县| 仙游县| 林西县| 咸宁市| 孙吴县| 益阳市| 恩施市| 余江县|