日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python批量删除注释_批量删除C和C++注释

發布時間:2024/1/23 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python批量删除注释_批量删除C和C++注释 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用Python語言 ,實現批量刪除C/C++類型注釋

1.目前支持去掉.h .hpp .c .cpp .java 這些以//或/**/為注釋符的源文件

2.支持遞歸遍歷目錄

3.當前版本為Python2.7版本,故只有安裝了Python2.7(或Python3.x以下版本的才可以直接使用,測試沒問題后將編出exe直接使用)

4.使用方法:

(1)建立源目錄,如d:\src(路徑中最好不要有中文);

(2)建立目標目錄,如d:\dst(路徑中最好不要有中文);

(3)將RemoveComment.py放到某處(路徑中最好不要有中文),如d:\RemoveComment.py

(4)打開cmd,輸入切換到(3)中RemoveComment.py所在的目錄(這里是d:\),輸入python RemoveComment.py d:\src d:\dst,回車

(5)去掉了注釋的源代碼將放在d:\dst目錄中

http://download.csdn.net/download/zp373860147/4361780

#coding:utf-8

importosimportsysdefDelComment(src, dst):

fSrc= open(src, 'rb')

fDst= open(dst, 'wb')

out=[]

STATE_NORMAL=0

STATE_BEGIN= 1STATE_LINE_COMMENT= 2STATE_BLOCK_COMMENT= 3STATE_END= 4State=STATE_NORMALwhile 1:

ReadInChar= fSrc.read(1)if ReadInChar == '':break;if State ==STATE_NORMAL:if ReadInChar == '/':

State=STATE_BEGINelse:

out.append(ReadInChar)elif State ==STATE_BEGIN:if ReadInChar == '/':

State=STATE_LINE_COMMENTelif ReadInChar == '*':

State=STATE_BLOCK_COMMENTelse:

State=STATE_NORMAL

out.append('/'+ReadInChar)elif State ==STATE_LINE_COMMENT:if ReadInChar == '\n':

State=STATE_NORMALelif State ==STATE_BLOCK_COMMENT:if ReadInChar == '*':

State=STATE_ENDelif State ==STATE_END:if ReadInChar == '/':

State=STATE_NORMAL

ReadInChar= fSrc.read(1)while ReadInChar == '\r' or ReadInChar == '\n':

ReadInChar= fSrc.read(1)

fSrc.seek(-1, 1)else:

State=STATE_BLOCK_COMMENT

fSrc.seek(-1, 1)

fDst.writelines(out)

fDst.flush()

fDst.close()

fSrc.close()defscanDir(srcpath, dstpath):ifos.path.isdir(srcpath):for files inos.listdir(srcpath):

fSrc=os.path.join(srcpath, files)ifos.path.isfile(fSrc):

scanDir(fSrc, dstpath)else:

fDst=os.path.join(dstpath, files)if notos.path.exists(fDst):

os.mkdir(fDst)

scanDir(fSrc, fDst)else:if srcpath.endswith(('.h','.c','.cpp','.hpp','.jave'):

DelComment(srcpath, os.path.join(dstpath, os.path.basename(srcpath)))if __name__ == '__main__':

paramlen=len(sys.argv)if paramlen!=3:print '輸入參數錯誤'sys.exit(1)

srcpath= sys.argv[1].rstrip('\\').rstrip('/')print 'src_path:' +srcpath

dstpath= sys.argv[2].rstrip('\\').rstrip('/')print 'dst_path:' +dstpathprint 'convert......'scanDir(srcpath, dstpath)print 'done!'

總結

以上是生活随笔為你收集整理的python批量删除注释_批量删除C和C++注释的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。