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写一个量化股票提醒系统

大家在没有阅读本文之前先看下python的基本概念, Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。 Python由Guido van Rossum于1989年底...

Python中的FTP通信模块ftplib的用法整理

Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件. FTP的工作流程及基本操作可参考协议RFC959. ftp登陆连...

解决python super()调用多重继承函数的问题

当类间继承关系很简单时,super()的使用很简单。 class A(object): def __init__(self): print('a') class B(A...

Linux下升级安装python3.8并配置pip及yum的教程

Linux下升级安装python3.8并配置pip及yum的教程

服务器的CentOS 7中自带的python版本是python-2.7.5,需要再安装一个 python-3.8.1 一、查看版本 安装前查看一下是否已经安装过python,我...

Python日志无延迟实时写入的示例

我在用python生成日志时,发现无论怎么flush(),文件内容总是不能实时写入,导致程序意外中断时一无所获。 以下是查到的解决方案(亲测可行): open 函数中有一个buffe...