Python脚本实现代码行数统计代码分享,python行数
Python脚本实现代码行数统计代码分享,python行数
之前用bash实现过(http://www.bkjia.com/article/61943.htm),不过那个不能在windows下使用,所以就写了个python版,也方便我以后使用……这里就不多介绍了,不懂的google下。
实现代码
复制代码 代码如下:
#!/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 ~
运行效果
好,就这些了,希望对你有帮助。
相关内容
- Windows和Linux下使用Python访问SqlServer的方法介绍,python
- Python和GO语言实现的消息摘要算法示例,pythongo
- Python实现生成简单的Makefile文件代码示例,
- Python创建xml的方法,Python创建xml方法
- Python接收Gmail新邮件并发送到gtalk的方法,pythongtalk
- Python获取DLL和EXE文件版本号的方法,
- Linux下使用python调用top命令获得CPU利用率,pythontop
- Python实现去除代码前行号的方法,python去除代码前行
- 使用graphics.py实现2048小游戏,graphics.py2048
- Python使用xlrd读取Excel格式文件的方法,
评论关闭