正在学习 Python, 做了个统计代码行的功能,

参考了网上很多前辈的帖子,添加了感觉还是比较实用的功能,

只是windows下测试了,而且代码文件编码形式是 utf-8的。

如果使用其它编码形式的话,估计修改下代码就行了。

功能特点:

是否统计空行

统计注释

设置忽略文件平

设置统计文件类型

根据不同文件类型,设置注释标签

以下,是代码:

 # Created by Aaron <xinlingever@outlook.com> in 2014
# This code is for Python 3.4 import os, sys
import traceback def strToBool(v):
return v.lower() in ("yes", "true", "t", "") exts = ['.js', '.html', '.css', '.h', 'cpp']
commentTags = ['//,/* */','<!-- -->','/* */', '//, /* */', '//, /* */','//, /* */']
commentTypeIndex = ['.js', '.html', '.css', '.cs', '.cpp', '.h']
ignor_folders = ['debug', 'release', 'ipch', 'output', '.svn', '.git', 'durango', 'bld', 'layout']
max_line_limit = 2000 count_empty_line = False
if len(sys.argv) > 1:
here = sys.argv[1]
else:
here = sys.argv[0] if(len(sys.argv) > 2):
exts = sys.argv[2].split()
if(len(sys.argv) > 3):
count_empty_line = strToBool(sys.argv[3]) def read_line_count(fname):
count = 0
comment_count = 0 is_in_multi_comment = False
multi_line_comment_tag_tailer = ''
for line in open(fname, encoding='utf-8').readlines():
if count_empty_line and len(line.strip()) == 0:
count += 1
else:
if len(line.strip()) > 0:
count += 1 # count comment
if(is_in_multi_comment):
comment_count += 1 if(line.find(multi_line_comment_tag_tailer) >= 0):
is_in_multi_comment = False
else:
ext = (fname[fname.rindex('.'):]).lower()
if ext not in commentTypeIndex:
continue for commentTag in commentTags[commentTypeIndex.index(ext)].split(','):
if(len(commentTag.split()) == 1):
# single line comment
if line.strip().startswith(commentTag):
comment_count += 1
else:
if(line.find(commentTag) >= 0):
comment_count += 1 else:
# multi line comment
multi_line_comment_tags = commentTag.split()
multi_line_comment_tag_header = multi_line_comment_tags[0]
multi_line_comment_tag_tailer = multi_line_comment_tags[1] if line.find(multi_line_comment_tag_header) >= 0:
comment_count += 1
is_in_multi_comment = True
if line.find(multi_line_comment_tag_tailer) >= 0:
is_in_multi_comment = False return count,comment_count
if __name__ == '__main__':
line_count = 0
file_count = 0
comment_line_count = 0 subFolderCount = 0;
for base, dirs, files in os.walk(here):
for file in files:
#print(file)
# Check the sub directorys
if file.find('.') < 0:
#print(file)
continue ext = (file[file.rindex('.'):]).lower()
try:
if ext in exts:
path = os.path.join(base,file)
relative_path = path[len(here):].replace(file,'').lower()
is_ignore = False
for ignorFolder in ignor_folders:
if relative_path.find(ignorFolder) >= 0:
is_ignore = True
break;
if is_ignore:
continue c,c2 = read_line_count(path)
if max_line_limit > 0:
if c > max_line_limit:
continue file_count += 1 print ("\t.%s : %d %d" % (path[len(here):], c, c2))
line_count += c
comment_line_count += c2
#else:
#print(file, "is not in list")
except Exception as N:
print(traceback.format_exc())
pass
print ('File count : %d' % file_count)
print ('Line count : %d' % line_count)
print ('Comment line count : %d' % comment_line_count)
print ('Comment rage is {:.2%}'.format ( comment_line_count / line_count))

Python 统计代码行的更多相关文章

  1. Python入门之用Python统计代码行

    Pycharm每天都要写很多代码,如何统计每天的代码行数呢?作为一个目标十万行的coder,要想想办法! 题目:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列 ...

  2. 利用python统计代码行

    参加光荣之路测试开发班已三月有余,吴总上课也总问“ 咱们的课上了这么多次了大家实践了多少行代码了?”.这里是一个一脸懵逼的表情.该怎么统计呢?一个个文件数当然不可取,能用代码解决的事咱们坚决不动手.最 ...

  3. python统计代码行数

    以前写了一个java的统计代码行数的小程序,最近在看python,于是就参考前辈的代码,写了一个统计文件夹下面各种程序的代码的小程序,这里贴出来供大家参考 参考链接: https://gist.git ...

  4. 007-使用python统计代码行数,空行以及注释

    # 自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来 1.打开文件方法 1.1 以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符 f ...

  5. python统计代码总行数(代码行、空行、注释行)

    我们在工作或学习代码的过程中,经常会想知道自己写了多少行代码,今天在项目环境写了个脚本统计了项目代码的数量. 功能: 1.统计代码总行数 2.统计空行数 3.统计注释行数 # coding=utf-8 ...

  6. Eclipse统计代码行数

    开发过程中,经常需要统计代码行数,这时可以通过Eclipse的Search功能来实现. 步骤: 1.在Package Explorer中选中需要统计的包: 2.单击菜单Search-->File ...

  7. 在Flash Builder或者Eclipse统计代码行数的方法

    在Flash  Builder或者Eclipse统计代码行数的方法如下图菜单栏--搜索--搜索文件

  8. 【Linux】常用命令-统计代码行数

    公司人员流动大,经常有新的维护任务,交接时喜欢看看新来的模块的代码量,那么问题来了, 如何统计代码行数? 1,最先想到的肯定是 wc. wc -l *.h 将查看[当前目录]下头文件的代码行数,输出结 ...

  9. c#统计代码行数

    小编,已经快学了两年编程了.昨天突发奇想,想统计下这些年到底写过多少行代码,于是做了一个这个小程序来统计代码行数.老规矩,先上图. 比较惭愧,写了两年只有2万多行.那我们还是进入下一项吧. 界面搭建我 ...

随机推荐

  1. linux中shell如何输出换行符

    echo -e "a\tb\tc\nd\te\tf" 加-e

  2. hdoj 4738 Caocao's Bridges【双连通分量求桥】

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 8-10-Exercise

    链接:第三次练习 A.ZOJ 3203  Light Bulb 这道题............哎~既可以用数学直接推导出来,也可以三分求,还可以二分求~~~~ NO1.数学公式 这种方法搞的不是很清楚 ...

  4. java树状结构之二叉树

    参考:http://blog.csdn.net/zhangerqing/article/details/8822476 前面已经提到过树和二叉树的相关概念内容,下面主要来介绍下关于二叉树的创建,遍历, ...

  5. Java 5种字符串拼接方式性能比较

    http://blog.csdn.net/kimsoft/article/details/3353849 import java.util.ArrayList; import java.util.Li ...

  6. Java的finally理解

    1.为什么要用finally 先看一个没有finally的异常处理try-catch语句: 如果count为要使用到的资源,而且用完要求释放此资源.那么我们能够把释放资源的语句放到try-catch后 ...

  7. Cocos2d-x 3.1.1 学习日志9--一“上一下其乐无穷”游戏开发系列一

    下载地址:http://app.mi.com/search?keywords=%E4%B8%80%E4%B8%8A%E4%B8%80%E4%B8%8B%E5%85%B6%E4%B9%90%E6%97% ...

  8. cocos2d-x plist+wen使用

    http://zhan.renren.com/tag?value=cocos2dx&from=template http://blog.csdn.net/zhanglongit/article ...

  9. (转)C#中的泛型

    来源:http://www.cnblogs.com/JimmyZhang/archive/2008/12/17/1356727.html .Net 1.1版本最受诟病的一个缺陷就是没有提供对泛型的支持 ...

  10. SQL Server数据类型

    转载:http://www.ezloo.com/2008/10/sql_server_data_type.html    数据类型是数据的一种属性,是数据所表示信息的类型.任何一种语言都有它自己所固有 ...