删除某文件夹(可包含其他文件夹)中Java文件的注释(Python)
2011-10-21 13:09
656 查看
#coding:utf-8
import os, sys
def help():
print """命令行格式: DelComment srcpath [dstpath]
srcpath为源目录或文件的路径;
detpath必须是已经存在的,它是存放转换后的目录的路径,默认为当前工作目录。
"""
def stripSlash(str):
return str.rstrip('\\').rstrip('/')
def copyRemoveAllComment(srcpath, dstpath):
#定义扫描的状态
STATE_SCAN = 0
STATE_IS_COMMENT_START = 1
STATE_IN_BLOCK_COMMENT = 2
STATE_IN_LINE_COMMENT = 3
STATE_IS_COMMENT_END = 4
#在dstpath中打开文件
srcfile = open(srcpath, 'rb')
dstfile = open(dstpath, 'wb')
#过滤注释
readState = STATE_SCAN
text = []
while True:
ch = srcfile.read(1)
if ch == '':
break;
if readState == STATE_SCAN:
if ch == '/':
readState = STATE_IS_COMMENT_START
else:
text.append(ch)
elif readState == STATE_IS_COMMENT_START:
if ch == '/':
readState = STATE_IN_LINE_COMMENT
elif ch == '*':
readState = STATE_IN_BLOCK_COMMENT
else:
readState = STATE_SCAN
text.append('/'+ch)
elif readState == STATE_IN_LINE_COMMENT:
if ch == '\n':
readState = STATE_SCAN
elif readState == STATE_IN_BLOCK_COMMENT:
if ch == '*':
readState = STATE_IS_COMMENT_END
elif readState == STATE_IS_COMMENT_END:
if ch == '/':
readState = STATE_SCAN
ch = srcfile.read(1)
while ch == '\r' or ch == '\n':
ch = srcfile.read(1)
srcfile.seek(-1, 1)
else:
readState = STATE_IN_BLOCK_COMMENT
srcfile.seek(-1, 1)
#写文件
dstfile.writelines(text)
dstfile.flush()
#关闭文件
srcfile.close()
dstfile.close()
def convert(srcpath, dstpath):
if os.path.isdir(srcpath):
dirfiles = os.listdir(srcpath)
for fn in dirfiles:
srcfp = os.path.join(srcpath, fn)
if os.path.isfile(srcfp):
convert(srcfp, dstpath)
else:
dstfp = os.path.join(dstpath, fn)
if not os.path.exists(dstfp):
os.mkdir(dstfp)
convert(srcfp, dstfp)
else:
if srcpath.endswith('.java'):
srcfn = srcpath
dstfn = os.path.join(dstpath, os.path.basename(srcpath))
copyRemoveAllComment(srcfn, dstfn)
if __name__ == '__main__':
argvlen = len(sys.argv)
#至少一个参数
if argvlen > 2+1 or argvlen <= 2-1:
print '参数错误!:-(\n'
help()
sys.exit(1)
elif not os.path.isfile(sys.argv[1]) and not os.path.isdir(sys.argv[1]) or argvlen == 1 + 2 and os.path.isfile(sys.argv[1]):
print '参数错误!:-(\n'
help()
sys.exit(2)
#获取源路径
srcpath = stripSlash(sys.argv[1])
print 'srcpath: ' + srcpath
#获取目标路径,
dstpath = ""
if argvlen == 1 + 2:
dstpath = stripSlash(sys.argv[2])
else:
dstpath = os.getcwd() + '\\delcomment'
if not os.path.exists(dstpath):
os.mkdir(dstpath)
#如果源路径是文件,则直接放在目标路径下
if os.path.isdir(srcpath):
dstpath = dstpath + '\\' + os.path.basename(srcpath) + 'delcomment'
if not os.path.exists(dstpath):
os.mkdir(dstpath)
print 'dstpath: ' + dstpath
#开始转换
convert(srcpath, dstpath)
import os, sys
def help():
print """命令行格式: DelComment srcpath [dstpath]
srcpath为源目录或文件的路径;
detpath必须是已经存在的,它是存放转换后的目录的路径,默认为当前工作目录。
"""
def stripSlash(str):
return str.rstrip('\\').rstrip('/')
def copyRemoveAllComment(srcpath, dstpath):
#定义扫描的状态
STATE_SCAN = 0
STATE_IS_COMMENT_START = 1
STATE_IN_BLOCK_COMMENT = 2
STATE_IN_LINE_COMMENT = 3
STATE_IS_COMMENT_END = 4
#在dstpath中打开文件
srcfile = open(srcpath, 'rb')
dstfile = open(dstpath, 'wb')
#过滤注释
readState = STATE_SCAN
text = []
while True:
ch = srcfile.read(1)
if ch == '':
break;
if readState == STATE_SCAN:
if ch == '/':
readState = STATE_IS_COMMENT_START
else:
text.append(ch)
elif readState == STATE_IS_COMMENT_START:
if ch == '/':
readState = STATE_IN_LINE_COMMENT
elif ch == '*':
readState = STATE_IN_BLOCK_COMMENT
else:
readState = STATE_SCAN
text.append('/'+ch)
elif readState == STATE_IN_LINE_COMMENT:
if ch == '\n':
readState = STATE_SCAN
elif readState == STATE_IN_BLOCK_COMMENT:
if ch == '*':
readState = STATE_IS_COMMENT_END
elif readState == STATE_IS_COMMENT_END:
if ch == '/':
readState = STATE_SCAN
ch = srcfile.read(1)
while ch == '\r' or ch == '\n':
ch = srcfile.read(1)
srcfile.seek(-1, 1)
else:
readState = STATE_IN_BLOCK_COMMENT
srcfile.seek(-1, 1)
#写文件
dstfile.writelines(text)
dstfile.flush()
#关闭文件
srcfile.close()
dstfile.close()
def convert(srcpath, dstpath):
if os.path.isdir(srcpath):
dirfiles = os.listdir(srcpath)
for fn in dirfiles:
srcfp = os.path.join(srcpath, fn)
if os.path.isfile(srcfp):
convert(srcfp, dstpath)
else:
dstfp = os.path.join(dstpath, fn)
if not os.path.exists(dstfp):
os.mkdir(dstfp)
convert(srcfp, dstfp)
else:
if srcpath.endswith('.java'):
srcfn = srcpath
dstfn = os.path.join(dstpath, os.path.basename(srcpath))
copyRemoveAllComment(srcfn, dstfn)
if __name__ == '__main__':
argvlen = len(sys.argv)
#至少一个参数
if argvlen > 2+1 or argvlen <= 2-1:
print '参数错误!:-(\n'
help()
sys.exit(1)
elif not os.path.isfile(sys.argv[1]) and not os.path.isdir(sys.argv[1]) or argvlen == 1 + 2 and os.path.isfile(sys.argv[1]):
print '参数错误!:-(\n'
help()
sys.exit(2)
#获取源路径
srcpath = stripSlash(sys.argv[1])
print 'srcpath: ' + srcpath
#获取目标路径,
dstpath = ""
if argvlen == 1 + 2:
dstpath = stripSlash(sys.argv[2])
else:
dstpath = os.getcwd() + '\\delcomment'
if not os.path.exists(dstpath):
os.mkdir(dstpath)
#如果源路径是文件,则直接放在目标路径下
if os.path.isdir(srcpath):
dstpath = dstpath + '\\' + os.path.basename(srcpath) + 'delcomment'
if not os.path.exists(dstpath):
os.mkdir(dstpath)
print 'dstpath: ' + dstpath
#开始转换
convert(srcpath, dstpath)
相关文章推荐
- 文件中包含文件和文件夹,只保留特定的文件,其他文件和文件夹删除
- java删除整个文件夹或文件(无视是否包含子文件夹)
- java实现文件夹以及包含的子文件夹或文件的删除
- 今天看到了一个用java实现复制,剪切,删除文件或文件夹的示例,感觉不错,欢迎学习(带详细注释)
- JAVA删除某一目录下的所有文件及文件夹(linnux环境)
- linux删除指定文件夹中某个文件除外的其他文件
- java删除指定目录中的文件或文件夹
- java创建和删除文件夹及文件
- python遍历文件夹并删除特定格式文件的示例
- java删除文件包含子文件
- Java遍历文件夹,重命名文件,删除文件,复制文件等操作
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- java 删除文件夹中的文件及文件夹
- 利用正则表达式统计某个某个文件夹下所有java文件的 空格行数、注释行数、和代码行数
- java 删除文件、删除文件夹操作
- java文件和文件夹复制、删除、移动操作
- 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集
- java 删除本地文件夹或文件
- Linux Shell删除某一个目录下的所有文件夹(保留其他文件)
- java定时删除文件夹及文件