How to remove annotations from Python files

Source: Internet
Author: User
Tags ini readfile split

The examples in this article describe how Python files remove annotations. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71-72 #!/usr/bin/python #-*-CODING:GBK-*-#writer: Xmnathan #py文件去注释 import re import os I Mport configparser python= ' cleannote ' def Readini (path,section,option): #文件路径, chapters, keywords #读取ini cf= Configparser.configparser () cf.read (path) value=cf.get () read the data type as an integer return value def directly (section,option) #如果用getint () Ispassline (strLine): #是否是可以忽略的行 #可忽略行的正则表达式列表 regularexpressions=["" "/". *#.*/' "" "," ""/". *#.*/" "" "," ""/'/'/'. *#.*/ '/'/' "", ""/"/"/". *#.*/"/"" "" "for one in Regularexpressions:zz=re.compile (one) if Re.search (zz,strline) ==none: Continue Else:return true# A match ignores return False def ReadFile (filename): #读取并处理文件 fobj=open (filename, ' r ') alllines=fobj.re Adlines () fobj.close () newstr= ' logstr= '/n%20s/n '% (filename.split ('//') [-1]) #输出的日志 nline=0 for Eachiline in Alllines: Index=eachline.find (' # ') #获取带注释句 ' # ' position index if index==-1 or nline<3 or Ispassline (eachline): If Eachiline.strip ()!= ': # Excluding pure-empty rows 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获取源文件夹及目标文件夹路径 inipath=os.getcwd () + '//' +ptname+ '. Ini ' Srcpath=readini (inipath,pyname, ' Srcpath ') #源文件夹 Descpath=readini (inipath,pyname, ' Descpath ') #目的文件夹 #如果目的文件夹不存在, created 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: Format of configuration file Cleannote.ini

?

1 2 3 [Cleannote] Srcpath=e:/test Descpath=e:/test/newfiles

Bulk remove the notes from the py file in the specified source folder and generate a copy with the specified destination folder

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.