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

yipeiwu_com5年前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中字符串对齐方法介绍

目的   实现字符串的左对齐,右对齐,居中对齐。 方法   字符串内置了以下方法:其中width是指包含字符串S在内的宽度,fillchar默认是空格,也可以指定填充字符 复制代码...

pygame实现雷电游戏雏形开发

pygame实现雷电游戏雏形开发

本文实例为大家分享了pygame实现雷电游戏开发代码,供大家参考,具体内容如下 源代码: stars.py #-*- coding=utf-8 -*- #!/usr/bin/pyt...

python web框架 django wsgi原理解析

前言 django wsgi python有个自带的wsgi模块 可以写自定义web框架 用wsgi在内部创建socket对象就可以了 自己只写处理函数就可以了 django只是web...

在Python中使用defaultdict初始化字典以及应用方法

Python中可以使用collections中的defaultdict类实现创建进行统一初始化的字典。这里总结两种常用一点的初始化方式,分别是初始化为list和int。 初始化为list...

python jieba分词并统计词频后输出结果到Excel和txt文档方法

python jieba分词并统计词频后输出结果到Excel和txt文档方法

前两天,班上同学写论文,需要将很多篇论文题目按照中文的习惯分词并统计每个词出现的频率。 让我帮她实现这个功能,我在网上查了之后发现jieba这个库还挺不错的。 运行环境: 安装...