脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/27 浏览:3 次 )

复制代码 代码如下:
#!/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访问sqlserver示例
下一篇:python基础教程之获取本机ip数据包示例
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap