python3怎么计算代码行数

607次阅读
没有评论

python3怎么计算代码行数

作为一个编程人员,每天除了测试外,很大一部分的时间都在敲代码中度过。那么有没有人想过我们写的代码到底有多少行呢?逐条去查数目显然是不现实的,不说浪费了大量的时间,而且我们人工的查数会出现失误,一行行密密麻麻的代码也看着头晕眼花的。下面小编就教大家python3中代码行数计算的方法。

 

首先判断传入参数是否为文件夹,不是则打印出提示,否则继续(无返回),获得目录后,yongos.listdir对路径下文件进行遍历,其中也包含文件夹,再次判断是否为文件夹,是的话则递归调用此函数,否则开始执行行数统计,这里用os.path.join将路径与文件名进行拼接,方便之后直接传给函数,逻辑很简单,无非是执行文件判断,判断是哪类文件,在调用对应的注释监测正则代码段进行抓取,抓取到则行数+1,空白行也是一样的原理,用strip(去除前后空格),然后行内内容为空则为空行,代码段即为总行数减去其他两类行数,最后在外层将所有文件对应的代码段累加即为total。

 

关键

函数内部是可以访问全局变量的,问题在于函数内部修改了变量,导致python认为它是一个局部变量。

所以,如果在函数内部访问并修改全局变量,应该使用关键字 global 来修饰变量。

import os 
import re 
#定义规则抓取文件中的python注释 
re_obj_py = re.compile('[(#)]') 
#定义规则抓取文件中的C语言注释 
re_obj_c = re.compile('[(//)(/*)(*)(*/)]') 
#判断是否为python文件 
def is_py_file(filename): 
if os.path.splitext(filename)[1] == '.py': 
return True 
else: 
return False 
#判断是否为c文件 
def is_c_file(filename): 
if os.path.splitext(filename)[1] in ['.c', '.cc', '.h']: 
return True 
else: 
return False 
#定义几个全局变量用于计算所有文件总和(全部行数、代码行数、空行数、注释行数) 
all_lines, code_lines, space_lines, comments_lines = 0, 0, 0, 0 
#判断是否为文件夹,不是则输出提示 
def count_codelines(dirpath): 
if not os.path.isdir(dirpath): 
print('input dir: %s is not legal!' % dirpath) 
return 
# 定义几个全局变量用于计算每个文件行数(全部行数、代码行数、空行数、注释行数) 
global all_lines, code_lines, space_lines, comments_lines 
#列出当前文件夹下的文件(包含目录) 
all_files = os.listdir(dirpath) 
for file in all_files: 
#将文件(目录)名与路径拼接 
file_name = os.path.join(dirpath, file) 
if os.path.isdir(file_name): 
count_codelines(file_name) 
else: 
temp_all_lines, temp_code_lines, temp_space_lines, temp_comments_lines = 0, 0, 0, 0 
f = open(file_name) 
for line in f: 
temp_all_lines += 1 
if line.strip() == '': 
temp_space_lines += 1 
continue 
if is_py_file(file_name) and re_obj_py.match(line.strip()): 
temp_comments_lines += 1 
if is_c_file(file_name) and re_obj_c.match(line.strip()): 
temp_comments_lines += 1 
temp_code_lines = temp_all_lines - temp_space_lines - temp_comments_lines 
print('%-15s : all_lines(%s)\t code_lines(%s)\t space_lines(%s)\t comments_lines(%s)' 
 % (file, temp_all_lines, temp_code_lines, temp_space_lines, temp_comments_lines)) 
all_lines += temp_all_lines 
code_lines += temp_code_lines 
space_lines += temp_space_lines 
comments_lines += temp_comments_lines 
if __name__ == '__main__': 
count_codelines('test') 
 print('\n**** TOTAL COUNT ****\nall_lines = %s\ncode_lines = %s\nspace_lines = %s\ncomments_lines = %s' % (all_lines, code_lines, space_lines, comments_lines))

 

在使用以上超长的计算代码之前,小伙伴们一定要理清计算行数的思路,再进行下一步的操作。毕竟如果计算的过程中出现问题,想要找出错误点是非常困难的。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-20发表,共计2425字。
新手QQ群:570568346,欢迎进群讨论 Python51学习