注意,我們只是簡單的提取// 以及/* */之間的內容。
如果 程序中出現了“/*”,會有bug
#!/usr/bin/env python
import sys
import re
def comment_finder(text):
pattern = re.compile( r'//.*?$|/\*.*?\*/', re.DOTALL | re.MULTILINE)
result = pattern.findall(text)
return result
def print_command(filename):
codefile = open(filename,'r')
commentfile = open(filename+".txt",'w')
lines=codefile.read()
codefile.close()
#the list of comments
list_of_comments = comment_finder(lines)
for comment in list_of_comments:
#print comment[0:2]
if comment[0:2] == "//":
comment_to_write = comment[2:]
else:
comment_to_write = comment[2:-2]
if len(comment_to_write)!=0:
commentfile.write(comment_to_write)
commentfile.write('\n')
commentfile.close()
if __name__ == "__main__":
for filename in sys.argv[1:]:
print_command(filename)
使用:
在linux下面 轉到當前目錄 ./get_comment.py *
或者 指定文件類型
./get_comment.py *.c
posted on 2012-12-03 08:35
luis 閱讀(1377)
評論(0) 編輯 收藏 引用 所屬分類:
Python