python实现统计代码行数的方法
作者:小小的我 时间:2021-06-23 11:24:17
本文实例讲述了python实现统计代码行数的方法。分享给大家供大家参考。具体实现方法如下:
'''
Author: liupengfei
Function: count lines of code in a folder iteratively
Shell-format: cmd [dir]
Attention: default file encode is utf8 and default file type is java-source-file. But users can customize this script by just modifing global variables.
'''
import sys
import os
import codecs
from _pyio import open
totalCount = 0;
fileType = '.java'
descLineBegin = '//'
descBlockBegin = r'/**'
descBlockEnd = r'*/'
fileEncode = 'utf-8'
def main():
DIR = os.getcwd()
if len(sys.argv) >= 2:
DIR = sys.argv[1]
if os.path.exists(DIR) and os.path.isdir(DIR):
print('target directory is %s' % DIR)
countDir(DIR)
print('total code line is %d' % totalCount)
else:
print('target should be a directory!')
def isFileType(file):
return len(fileType) + file.find(fileType) == len(file)
def countDir(DIR):
for file in os.listdir(DIR):
absPath = DIR + os.path.sep + file;
if os.path.exists(absPath):
if os.path.isdir(absPath):
countDir(absPath)
elif isFileType(absPath):
try:
countFile(absPath)
except UnicodeDecodeError:
print(
'''encode of %s is different, which
is not supported in this version!'''
)
def countFile(file):
global totalCount
localCount = 0
isInBlockNow = False
f = codecs.open(file, 'r', fileEncode);
for line in f:
if (not isInBlockNow) and line.find(descLineBegin) == 0:
pass;
elif (not isInBlockNow) and line.find(descBlockBegin) >= 0:
if line.find(descBlockBegin) > 0:
localCount += 1
isInBlockNow = True;
elif isInBlockNow and line.find(descBlockEnd) >= 0:
if line.find(descBlockEnd) + len(descBlockEnd) < len(line):
localCount += 1
isInBlockNow = False;
elif (not isInBlockNow) and len(line.replace('\\s+', '')) > 0:
localCount += 1
f.close()
totalCount += localCount
print('%s : %d' % (file, localCount))
if __name__ == '__main__':
main();
希望本文所述对大家的Python程序设计有所帮助。
标签:python,统计
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MYSQL创建触发程序的方法
2009-07-30 08:38:00
Pandas 连接合并函数merge()详解
2021-10-28 05:13:44
MYSQL教程:索引和查询优化程序
2009-02-27 15:52:00
python神经网络tf.name_scope和tf.variable_scope函数区别
2021-01-24 13:10:48
基于PHP读取csv文件内容的详解
2023-11-16 04:17:48
python 计算一个字符串中所有数字的和实例
2022-07-19 01:42:33
如何列举Error的所有对象?
2010-01-12 20:01:00
XHTML与HTML之间的7个区别
2009-05-20 10:13:00
python 数据类(dataclass)的具体使用
2022-11-08 09:36:27
python使用pil生成缩略图的方法
2022-06-07 13:07:44
PHP addAttribute()函数讲解
2023-06-06 09:03:45
![](https://img.aspxhome.com/file/2023/2/55412_0s.png)
pytorch中的model=model.to(device)使用说明
2023-02-23 15:07:48
利用Python破解斗地主残局详解
2021-06-04 06:16:49
![](https://img.aspxhome.com/file/2023/2/92342_0s.jpg)
Python 使用SFTP和FTP实现对服务器的文件下载功能
2023-10-29 09:39:40
Python基于Matplotlib库简单绘制折线图的方法示例
2021-10-26 15:01:12
![](https://img.aspxhome.com/file/2023/5/84055_0s.png)
Django限制API访问频率常用方法解析
2022-06-24 18:20:13
python同时给两个收件人发送邮件的方法
2021-10-23 07:31:36
详解Python中的时间格式的读取与转换(time模块)
2021-01-09 17:02:38
![](https://img.aspxhome.com/file/2023/6/100936_0s.png)
IE7 与 IE6 的模式窗口尺寸差异
2008-03-06 13:49:00
![](https://img.aspxhome.com/file/UploadPic/20083/6/200836135432888s.gif)
Python操作redis实例小结【String、Hash、List、Set等】
2021-07-13 12:24:03