python-统计python文件代码行数

题目:

一个目录里面多个python程序文件,统计一下里面有多少行代码。即分别列出:代码、空行、注释的行数。其中注释包括单行注释(#) 多行注释(’’’/""")

代码:

# -*- coding=utf-8 -*-
# name: nan chen
# date: 2021/3/25 15:03
import os


# 统计代码行数
def count_codes(path):
    # 返回指定的文件夹包含的文件/文件夹的名字的列表
    file_list = os.listdir(path)
    # 改变当前工作路径
    os.chdir(path)
    nn = ("'''", '"""')
    totalcodes, totalnulls, totalnotes = 0, 0, 0
    for file in file_list:
        if file.endswith('.py'):
            total, codes, nulls, notes = 0, 0, 0, 0
            # 标记多行注释
            isMutinotes = False
            lines = open(file, encoding='utf-8').readlines()
            for line1 in lines:
                line = line1.strip()
                if line.startswith('#'):
                    notes += 1
                elif line.startswith(nn):
                    if line.endswith(nn) and len(line) > 3:
                        notes += 1
                        isMutinotes = False
                        continue
                    if not isMutinotes:
                        isMutinotes = True
                        notes += 1
                    elif isMutinotes:
                        isMutinotes = False
                        notes += 1
                elif isMutinotes:
                    notes += 1
                elif not isMutinotes and len(line) == 0:
                    nulls += 1
                else:
                    codes += 1
            total = nulls + codes + notes
            totalcodes = totalcodes + total  # 代码行数
            totalnulls = totalnulls + nulls
            totalnotes = totalnotes + notes
            print("%s 有代码%d行 空行%d行 注释%d行" % (file, total, nulls, notes))
    print("%s目录下python程序文件共有代码%d行 空行%d行 注释%d行" % (path, totalcodes, totalnulls, totalnotes))


if __name__ == '__main__':
    # count_codes("D:\computerVision")
    count_codes("D:\pycode")

结果:

python-统计python文件代码行数_第1张图片

你可能感兴趣的:(信息分析与预测,python)