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

yipeiwu_com6年前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 ~

相关文章

PyQt5多线程刷新界面防假死示例

PyQt5多线程刷新界面防假死示例

在做GUI界面时我们希望后台任务能够与UI分开,在PyQt中,主线程用来重绘界面。而子线程里边的实时处理结果需要反馈到界面,子线程里边不能执行界面更新操作。 wxpython多线程刷新界...

浅谈python新式类和旧式类区别

python的新式类是2.2版本引进来的,我们可以将之前的类叫做经典类或者旧式类。 为什么要在2.2中引进new style class呢?官方给的解释是: 为了统一类(class)和类...

Python 命令行非阻塞输入的小例子

 随手google咗一下,基本上都用select实现非阻塞监听,但问题是,监听的是用select之后是不能像getchar()那样,即时收到单个字符的输入,必须要等待回车。 &...

Python实现PS图像抽象画风效果的方法

Python实现PS图像抽象画风效果的方法

本文实例讲述了Python实现PS图像抽象画风效果的方法。分享给大家供大家参考,具体如下: 今天介绍一种基于图像分割和color map 随机采样生成一种抽象画风的图像特效,简单来说,就...

pyqt弹出新对话框,以及关闭对话框获取数据的实例

如下所示: from PyQt4 import QtGui,QtCore import sys class Web_Browser(QtGui.QDialog): def __i...