用于统计项目中代码总行数的Python脚本分享

yipeiwu_com6年前Python基础

最近需要统计一下项目中代码的总行数,写了一个Python小程序,不得不说Python是多么的简洁,如果用Java写至少是现在代码的2倍。
[code]
import os
path="/Users/rony/workspace/ecommerce/ecommerce/hot-deploy/"
global totalcount
totalcount =0
def cfile (path):
    allfiles = os.listdir(path)
    for file in allfiles:
        child = os.path.join(path,file)
        if os.path.isdir(child):
            cfile(child)
        else:
            filename,fileext= os.path.splitext(child)
            print(fileext)
            #file type need to calculate
            if fileext in ['.java', '.jsp', '.html', '.htm', '.xml', '.sql', '.js', '.ftl', '.css','.groovy'] :
                countf = len(open(child,'rU').readlines())
                global totalcount
                totalcount=totalcount+countf;
                print(child)
                print(countf)
cfile(path)
print(totalcount)

关于代码上的分析就到这里,例子比较简单。

相关文章

Python编写检测数据库SA用户的方法

本文讲述一个用Python写的小程序,用于有注入点的链接,以检测当前数据库用户是否为sa,详细代码如下: # Code by zhaoxiaobu Email: little.bu@...

python递归实现快速排序

快速排序(QuickSort)是对冒泡排序的一种改进: 基本思想: 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两...

Python中使用装饰器来优化尾递归的示例

尾递归简介 尾递归是函数返回最后一个操作是递归调用,则该函数是尾递归。 递归是线性的比如factorial函数每一次调用都会创建一个新的栈(last-in-first-out)通过不断的...

Python玩转Excel的读写改实例

摘要: 利用xlrd读取excel 利用xlwt写excel 利用xlutils修改excel 利用xlrd读取excel 先需要在命令行中pip install xlr...

python 判断linux进程,并杀死进程的实现方法

如下所示: ''' @author: Jacobpc ''' import os import sys import subprocess def get_process_id(...