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 ~
运行效果
好,就这些了,希望对你有帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mysql安装注意事项、安装失败的五个原因分析
sql 常用技巧整理
python实现一个摇骰子小游戏
如何把Oracle 数据库从 RAC 集群迁移到单机环境
python playwright之元素定位示例详解
python的pyecharts绘制各种图表详细(附代码)
![](https://img.aspxhome.com/file/2023/4/76764_0s.jpg)
python解析库Beautiful Soup安装的详细步骤
![](https://img.aspxhome.com/file/2023/4/92094_0s.png)
pandas实现导出数据的四种方式
python两种获取剪贴板内容的方法
![](https://img.aspxhome.com/file/2023/8/127118_0s.png)
在应用环境中如何构造最优的数据库模式
PHP商品秒杀问题解决方案实例详解【mysql与redis】
一篇文章带你了解python标准库--datetime模块
![](https://img.aspxhome.com/file/2023/0/112400_0s.png)
Pyinstaller+Pipenv打包Python文件的实现示例
![](https://img.aspxhome.com/file/2023/4/82594_0s.png)
Python随机函数库random的使用方法详解
![](https://img.aspxhome.com/file/2023/6/70106_0s.png)
python如何使用replace做多字符替换
js拖动div 当鼠标移动时整个div也相应的移动
Go语言常用条件判断空值逻辑的使用
MySQL高效模糊搜索之内置函数locate instr position find_in_set使用详解
git 优雅的撤销中间某次提交方法
![](https://img.aspxhome.com/file/2023/5/131965_0s.png)