python实现代码行数统计示例分享

yipeiwu_com5年前Python基础

复制代码 代码如下:

#!/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 ~

相关文章

python实现从字典中删除元素的方法

本文实例讲述了python实现从字典中删除元素的方法。分享给大家供大家参考。具体分析如下: python的字典可以通过del方法进行元素删除,下面的代码详细演示了这一过程 # Cre...

Django 反向生成url实例详解

Django中提供了一个关于URL的映射的解决方案, 1.客户端的浏览器发起一个url请求,Django根据URL解析,把url中的参数捕获,调用相应的试图,获取相应的数据,然后返回给客...

Python开发的单词频率统计工具wordsworth使用方法

Python开发的单词频率统计工具wordsworth使用方法

使用方法: python wordsworth --filename textfile.txt python wordsworth -f textfile.txt 分析结果:...

python中使用urllib2获取http请求状态码的代码例子

采集内容常需要得到网页返回的验证码做进一步处理 下面代码是用python写的用来获取网页http状态码的脚本 #!/usr/bin/python # -*- coding: utf-...

Python时间序列缺失值的处理方法(日期缺失填充)

前言 因近期进行时间序列分析时遇到了数据预处理中的缺失值处理问题,其中日期缺失和填充在网上没有找到较好较全资料,耗费了我一晚上工作时间,所以下面我对这次时间序列缺失值处理学习做了以下小...