代码行数及字数统计脚本

还说啥呀,直接上代码趴~

# coding=utf-8

"""
统计源码行数及字数脚本
    by Joy
"""

import os
import glob
# 给定源码文件夹
dirPath = r"D:\SVN\Python\ftp"
# 统计文件类型的元组,可以依据格式自行添加类型
suffixTuple = ("py","java","json","c")

def counter():
    global dirPath
    # 路径格式纠错
    if dirPath.endswith("\\"):
        dirPath = dirPath.strip("\\")
    elif dirPath.endswith("/"):
        dirPath = dirPath.strip("/")
    if not os.path.isdir(dirPath):
        print "目标文件夹不存在...请检查"

    # 行数
    lineCounter = 0
    # 字符数
    charCounter = 0

    for root, dirs, files in os.walk(dirPath):
        index = 0
        while index < len(suffixTuple):
            # 获取文件夹下所有匹配文件的绝对路径
            for filePath in glob.glob(root+r"\*.{}".format(suffixTuple[index])):
                if filePath and os.path.isfile(filePath):
                    with open(filePath,"r") as fr:
                        for line in fr.readlines():
                            # 简单对python及java中的注释行及空白行做了个过滤,有待改进
                            if not line.startswith("#") or line.startswith("//") or line.strip()=="":
                                lineCounter += 1
                                charCounter += len(line)
                else:
                    print "{}文件不存在...".format(filePath)
            index +=1

    print "源码行数为:{} ,字符数为:{}".format(lineCounter,charCounter)


if __name__ == "__main__":
    counter()


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值