用Python编写C\C++代码统计工具
【摘要】 代码参考:https://foofish.net/how-to-count-code.html
import os def parse(path): in_multi_comment = False comments,blanks,codes = 0,0,0 with open(path,'r') as fp: for scline in fp.readli...
代码参考:https://foofish.net/how-to-count-code.html
-
import os
-
-
def parse(path):
-
in_multi_comment = False
-
comments,blanks,codes = 0,0,0
-
-
with open(path,'r') as fp:
-
for scline in fp.readlines():
-
scline = scline.strip()
-
-
if scline == '' and not in_multi_comment:
-
blanks += 1
-
elif scline.startswith('//') or \
-
(scline.startswith("/*") and scline.endswith("*/")) or \
-
(in_multi_comment and not scline.endswith("*/")):
-
comments += 1
-
elif scline.startswith("/*") or scline.endswith("*/"):
-
in_multi_comment = not in_multi_comment
-
comments += 1
-
else:
-
codes += 1
-
return {"comments":comments,"blanks":blanks,"codes":codes}
-
-
-
def counter(path):
-
if os.path.isdir(path):
-
comments,blanks,codes = 0,0,0
-
list_dirs = os.walk(path)
-
for root,dirs,files in list_dirs:
-
for f in files:
-
file_path = os.path.join(root,f)
-
if file_path.endswith(".c") or file_path.endswith(".h"):
-
stats = parse(file_path)
-
comments += stats.get("comments")
-
blanks += stats.get("blanks")
-
codes += stats.get("codes")
-
return {"comments":comments,"blanks":blanks,"codes":codes}
-
else:
-
return parse(path)
-
-
if __name__ == '__main__':
-
print (counter("你的代码路径"))
文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq523176585/article/details/78869797
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)