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单例模式的使用方法。分享给大家供大家参考。具体如下: 方法一 复制代码 代码如下:import threading    class S...

Python实现PS图像明亮度调整效果示例

Python实现PS图像明亮度调整效果示例

本文实例讲述了Python实现PS图像明亮度调整效果。分享给大家供大家参考,具体如下: 这里用 Python 实现 PS 图像调整中的明度调整: 我们知道,一般的非线性RGB亮度调整只是...

python3.7.0的安装步骤

python3.7.0的安装步骤

如何安装Python的操作步骤: 1.第一步先去python的官方网站下载python的安装包 地址: https://www.python.org/downloads/ 根据自己的系...

使用Python的turtle模块画国旗

使用Python的turtle模块画国旗

Python的turtle模块画国旗主要用到两个函数:draw_rentangle和draw_star。 至于函数的调用就和我们学的C,C++是一样的。对于turtle画国旗的程序中,首...

Python数据结构与算法之完全树与最小堆实例

本文实例讲述了Python数据结构与算法之完全树与最小堆。分享给大家供大家参考,具体如下: # 完全树 最小堆 class CompleteTree(list): def sif...