python 查询代码量

 

# coding=utf-8
import os
import time

BASEDIR = r'C:\Users\Downloads\Python-master\Python-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 != '\n':  # 过滤掉空行
            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)

 

  

posted @ 2020-12-09 13:39  流年中渲染了微笑  阅读(110)  评论(0编辑  收藏  举报