统计代码行数的python小工具

一个用python实现的统计代码行数的小工具

先立个flag, 新年新目标,希望2018年代码行数可以写到五万行。

实现功能

  • 计算出某一目录以及子目录下代码文件的行数
  • 在计算代码的过程中,只对标准命名的文件进行统计,如[文件名.文件类型]
  • 排除了以“#”开头的包含文件,宏定义等,如#include, #define, #pragma等
  • 排除了c,cpp文件中的“//”, “//”等的注释
  • 排除了python文件中import, from 等开头的导入

使用方法

新建countLines.py文件,复制代码到文件中,将其放在想计算行数的代码目录下,直接使用Python运行即可算出该目录以及所有子目录下代码文件的行数。

  1. 将该python文件命名为countLines.py,放到想计算代码行数的文件目录下
    文件目录
  2. 其中helloworld.c文件内容如下:
    helloworld.c文件内容
  3. 用python.exe直接打开该文件或者使用python countLines.py,运行文件即可。
    计算结果
    我们可以看到,统计代码行的时候我们排除了两个空行以及开头的包含文件行,因此有效代码行为5行

代码实现

#!/usr/bin/env python
# -- coding: utf-8 --
# @Time    : 2018/3/5 13:55
# @Author  : likewind
# @mail    : likewind1993@163.com
# @File    : countLines.py
# @Software: sky-studio.cn
import os
'''
返回每个文件行数,其中行数不包括以“#”开头的包含文件,宏定义等,
排除了c,cpp文件中的“//”, “/*...*/”等的注释,
排除了python文件中import, from 等开头的导入
'''

def get_lines(file_name):
    f = open(file_name)
    #flag用于处理c,cpp中“/*...*/”多行注释
    flag = False
    count = 0
    while True:
        #读取文件并去除开头的空格,制表符
        line = f.readline()
        line = line.lstrip(' \t')
        if not line:
            break
        #如果该行有“#”, “import”等打头的字符,忽略该行
        if flag == False:
            if line[0:1] == "#" or line[0:6] == "import" or line[0:4] == "from" or line == "\n" or line[0:2] == "//":
                continue
        #如果该行存在“/*”并且不存在“*/”,表明多行注释未在一行结束,flag=True
        if line.find("/*") != -1 :
            if line.find("*/") != -1:
                continue
            else:
                flag = True
                continue
        #如果flag=True,表明处于多行注释中,判断是否有“*/”结尾
        if flag == True :
            if line.find("*/") != -1:
                flag = False
                if line[-2:] != "*/":
                    count = count+1
            continue
        #排除以上条件后,行数增加一
        count = count+1
    f.close()
    return count
'''
计算该文件目录下所有符合条件的行数
'''
def count_lines(file_dir):
    #total_lines表示总行数,file_nums表示总文件数
    total_lines = 0
    file_nums = 0
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            #不计算本文件的行数
            if file == "countLines.py":
                continue
            #只计算规范命名文件, 如[文件名.文件类型]
            file_type = file.split('.')
            if len(file_type) > 1 :
                #如果想计算其他类型的文件,可以在这里进行修改
                if file_type[1] not in ["py", "c", "cc", "cpp", "h"]:
                    continue
            else:
                continue
            file_name = root +"\\" + file
            lines = get_lines(file_name)
            total_lines = total_lines + lines
            print file_name + " contains lines : " + repr(lines)
            file_nums = file_nums + 1
    #输出结果
    print "------------------------------------"
    print "Total Files : " + repr(file_nums)
    print "Total lines : " + repr(total_lines)
    print "------------------------------------"

if __name__ == '__main__':
    cur_path = os.path.split(os.path.realpath(__file__))[0]
    count_lines(cur_path)
    raw_input("Press any key to exit...")
Python代码行数统计工具有很多种,以下是其中几种常用的工具: 1. cloc:cloc是一款开源的多语言代码行数统计工具,可以统计各种编程语言的代码行数包括Python。它可以生成详细的代码统计报告,包括代码行数空行数、注释行数等。使用cloc,您可以通过命令行或者图形界面界面来统计Python代码行数。 2. Pygount:Pygount是一个基于Python代码行数统计工具,它可以统计各种编程语言的代码行数包括Python。Pygount提供了一个简单易用的命令行界面,可以输出代码行数统计结果。 3. SLOCCount:SLOCCount是一个流行的代码行数统计工具,可以统计多种编程语言的代码行数。它可以生成详细的代码行数统计报告,包括代码行数空行数、注释行数等。SLOCCount可以通过命令行界面或者图形界面来统计Python代码行数。 4. Radon:Radon是一个Python代码复杂性分析工具,它也可以用来统计代码行数。Radon提供了各种度量方法,包括LOC (Lines of Code)、LLOC (Logical Lines of Code)、SLOC (Source Lines of Code)等,可以帮助您更全面地了解代码的复杂性和行数。 以上是几种常用的Python代码行数统计工具,您可以根据自己的需求和喜好选择适合的工具进行使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python实现代码行数统计工具](https://blog.csdn.net/weixin_30664539/article/details/99054675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [chatgpt赋能pythonPython代码行数统计-统计Python代码行数的常用工具与使用方法](https://blog.csdn.net/findyi123/article/details/130980303)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值