Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Extract the comments from the C/C++ style source code to the same name add .cmt ext name. Usage is `python topdir .ext1 .ext2 ...`. You can set the encoding to read and write file correctly. Reference the source here
#!/usr/bin/env python
import sys
import re
import os.path
import codecs
import os
encoding = 'cp932'
def comment_finder(text):
pattern = re.compile( r'//.*?$|/\*.*?\*/', re.DOTALL | re.MULTILINE)
result = pattern.findall(text)
return result
def print_command(filename):
codefile =,'r', encoding)
commentfile ="comments.cmt",'a', encoding)
#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:]
comment_to_write = comment[2:-2]
if comment_to_write.endswith("\r"):
comment_to_write = comment_to_write[0:-1]
if len(comment_to_write)!=0:
def visitor(filters, dirname, names):
mynames = filter(lambda n : os.path.splitext(n)[1].lower() in filters, names)
for name in mynames:
fname = os.path.join(dirname, name)
if not os.path.isdir(fname):
print "Analyzing " + fname
if __name__ == "__main__":
if len(sys.argv) < 3:
print "Usage:"
print " python topdir .ext1 [.ext2]"
print "e.g.:"
print " python . .swift .m .h"
topdir = sys.argv[1]
filters = sys.argv[2:]
os.path.walk(topdir, visitor, filters)
print "Comments output to comments.cmt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment