python 查询代码量

python统计查询代码量

# coding=utf-8
import os
import time

BASEDIR = r'C:Users乔晓强DownloadsPython-masterPython-master'
filelists = []
# 指定想要统计的文件类型
whitelist = ['py']


# 遍历文件, 递归遍历文件夹中的所有
def get_file(basedir):
    global filelists
    for parent, dirnames, filenames in os.walk(basedir):
        # for dirname in dirnames:
        #    getFile(os.path.join(parent,dirname)) #递归
        for filename in filenames:
            ext = filename.split('.')[-1]
            # 只统计指定的文件类型,略过一些log和cache文件
            if ext in whitelist:
                filelists.append(os.path.join(parent, filename))


# 统计一个文件的行数
def count_line(filename):
    count = 0
    for file_line in open(filename, encoding="utf-8").readlines():
        if file_line != '' and file_line != '
':  # 过滤掉空行
            count += 1
    return count


if __name__ == '__main__':
    startTime = time.clock()
    get_file(BASEDIR)
    total_line = 0
    for file_list in filelists:
        total_line += count_line(file_list)
    print('total lines:', total_line)

  

原文地址:https://www.cnblogs.com/a438842265/p/14108068.html