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实现代码行数统计示例分享请关注haodaima.com其它相关文章!

标签: 行数 python