基本信息
源码名称:python统计代码行数
源码大小:1.71KB
文件格式:
开发语言:Python
更新时间:2019-05-09
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍
下载后 需要将文件后缀改为 .py后运行

# -*- coding: utf-8 -*-
import os

# 需要统计的文件类型
exts = ['.py']
# 排除的文件夹
excludes = ["E:\PycharmProjects\test002\.idea"]


def get_line_count(file_name):
    line_count = 0
    with open('code_line_count.txt', 'a') as f:
        f.write('file_name : %s \n' % file_name)
    with open(file_name, 'r', encoding='utf8') as f:
        for file_line in f.readlines():
            file_line = file_line.strip()
            if not len(file_line) or file_line.startswith('//'):
                continue
            line_count  = 1
    with open('code_line_count.txt', 'a') as f:
        f.write('line count::%s\n' % line_count)
    return line_count


if __name__ == '__main__':
    with open('code_line_count.txt', 'w') as f:
        f.write('\n')
    count = 0
    file_count = 0
    for root, dirs, files in os.walk(os.getcwd()):
        for f in files:
            file_name = (root   '\\'   f)
            print(file_name)

            if os.path.dirname(file_name).split()[0] not in excludes:
                if os.path.splitext(f)[1]:
                    ext = f[f.rindex('.'):]
                    try:
                        if exts.index(ext) >= 0:
                            file_count  = 1
                            c = get_line_count(file_name)
                            count  = c
                            with open('code_line_count.txt', 'a') as f:
                                f.write('total count:%s\n' % count)
                    except:
                        pass

    with open('code_line_count.txt', 'a') as f:
        f.write('\n')
        f.write('--------------------------------------\n')
        f.write('total file count:%d\n' % file_count)
        f.write('total line count:%d\n' % count)