呵呵,刚学,俺写了一个去掉代码中注释的工具,python写的
秀一下,欢迎赐教!!!
# -*- coding: cp936 -*-
"""
this function can visit every file in all the folders of the specific dir
"""
import os,string
def search(uDir, aDir):
aFiles = os.listdir(uDir)
i = 0
while i < len(aFiles):
aFile = aFiles
if os.path.isfile(os.path.expanduser(uDir + aFile)):
print 'this is a file, name is ' + aFile
delNotes(os.path.expanduser(uDir + aFile), os.path.expanduser(aDir + aFile))
else:
nextuDir = uDir + aFile + '/'
nextaDir = aDir + aFile + '/'
print 'the folder ' + uDir
if not os.path.exists(os.path.expanduser(nextaDir)):
os.mkdir(os.path.expanduser(nextaDir))
print nextaDir
search(nextuDir, nextaDir)
i = i + 1
"""
visit every file of the specific directory, and delete thhose parts used for noting the logic
"""
import os,string
def delNotes(upFile, aimFile):
rFlag = 0
try:
f = open(upFile, 'r')
f2 = open(aimFile, 'w')
except IOError:
print 'open ' + upFile + ' error'
sys.exit()
print ""
print "Now begin to deal with the file, its name is " + upFile
print "***************************************************"
line = f.readline()
while line:
# 首先查找多行注释标记 /* */
fm = string.find(line, '/*')
# 找到多行注释标记
if fm != -1: #查找到/*
rFlag = 1
fm = string.find(line, '*/') #继续查找*/
if fm != -1: #若找到,则置标志为true,继续读下一行
rFlag = 0
#line = f.readline()
else: #否则,继续向下寻找 */,直到找到为止
line = f.readline()
while line:
fm = string.find(line, '*/')
if fm != -1: # 找到*/,置标志为true,退出循环继续向下处理
rFlag = 0
break
line = f.readline()
if not line:
print '文件标注不正确,退出'
# 若没有多行注释标记,则查找单行注释标记//
else:
fm = string.find(line, '//')
fm1 = string.find(line, 'System.out.print(')
if fm == -1 and fm1 == -1 and ( not rFlag): #本行不存在单行注释,则直接输出
f2.write(line)
elif fm != -1 and fm1 == -1 and ( not rFlag):
f2.write(line[:fm] + '\n')
# 继续读下一行
line = f.readline()
f.close()
f2.close()
if __name__ == "__main__":
search('/xx/','/yy/')
|