您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Python脚本实现代码行数统计代码分享

2024/5/17 0:42:07发布16次查看
之前用bash实现过(http://www.bitscn.com/article/61943.htm),不过那个不能在windows下使用,所以就写了个python版,也方便我以后使用……这里就不多介绍了,不懂的google下。
实现代码
代码如下:
#!/usr/bin/python
'''
        file      : count.py
        author    : mike
        e-mail    : mike_zhang@live.com
'''
import sys,os
extens = [.c,.cpp,.hpp,.h]
linescount = 0
filescount = 0
def funcount(dirname):
    global extens,linescount,filescount
    for root,dirs,filenames in os.walk(dirname):
        for f in filenames:
            fname = os.path.join(root,f)
            try :
                ext = f[f.rindex('.'):]
                if(extens.count(ext) > 0):
                    print 'support'
                    filescount += 1
                    print fname
                    l_count = len(open(fname).readlines())
                    print fname, : ,l_count
                    linescount += l_count
                else:
                    print ext, : not support
            except:
                print error occur!
                pass
if len(sys.argv) > 1 :
    for m_dir in sys.argv[1:]:       
        print m_dir
        funcount(m_dir)
else :
    funcount(.)
print files count : ,filescount
print lines count : ,linescount
raw_input(press enter to continue)
使用方法
1、针对本目录
代码如下:
./count.py
2、统计多个目录
代码如下:
./count.py /tmp ~
运行效果
好,就这些了,希望对你有帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product