Python脚本实现代码行数统计代码分享

作者:junjie 时间:2023-02-26 00:24:13 

之前用bash实现过(https://www.jb51.net/article/61943.htm),不过那个不能在windows下使用,所以就写了个python版,也方便我以后使用……这里就不多介绍了,不懂的google下。

实现代码


#!/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,代码行数,统计
0
投稿

猜你喜欢

  • Mysql安装注意事项、安装失败的五个原因分析

    2024-01-22 14:16:48
  • sql 常用技巧整理

    2011-11-03 17:10:14
  • python实现一个摇骰子小游戏

    2021-11-06 05:49:26
  • 如何把Oracle 数据库从 RAC 集群迁移到单机环境

    2024-01-28 06:29:05
  • python playwright之元素定位示例详解

    2023-06-14 01:08:41
  • python的pyecharts绘制各种图表详细(附代码)

    2021-12-29 16:51:13
  • python解析库Beautiful Soup安装的详细步骤

    2023-06-23 04:29:21
  • pandas实现导出数据的四种方式

    2023-01-03 14:57:57
  • python两种获取剪贴板内容的方法

    2021-03-05 11:18:56
  • 在应用环境中如何构造最优的数据库模式

    2009-04-01 14:28:00
  • PHP商品秒杀问题解决方案实例详解【mysql与redis】

    2023-09-27 10:49:04
  • 一篇文章带你了解python标准库--datetime模块

    2022-08-21 03:42:04
  • Pyinstaller+Pipenv打包Python文件的实现示例

    2021-06-11 01:49:51
  • Python随机函数库random的使用方法详解

    2021-06-07 16:16:23
  • python如何使用replace做多字符替换

    2022-10-23 01:26:59
  • js拖动div 当鼠标移动时整个div也相应的移动

    2024-04-22 22:17:20
  • Go语言常用条件判断空值逻辑的使用

    2024-04-25 15:10:07
  • MySQL高效模糊搜索之内置函数locate instr position find_in_set使用详解

    2024-01-27 00:03:23
  • git 优雅的撤销中间某次提交方法

    2023-12-27 11:12:50
  • BootStrap数据表格实例代码

    2024-05-09 14:56:15
  • asp之家 网络编程 m.aspxhome.com