统计py文件或目录代码行数

bug:当遇到3个“”“时 可能会将下面的代码不计入代码总行数

import os
def count_path(path,countcode):
    if os.path.isdir(path):
        file_list = os.walk(path)
        for file_path in file_list:
            x, _, y = file_path
            for i in y:
                if i.split('.')[-1] == 'py':
                    count_path(os.path.join(x, i),countcode)
    if os.path.isfile(path):
        count_code(path,countcode)
def count_code(path,countcode):
    flag = True
    count = 0
    with open(f'{path}', encoding='utf-8') as fr:
        for i in fr:
            if i.startswith('#') and float:
                continue
            if i == '\n' and float:
                continue
            if (i.startswith('\'\'\'') or i.startswith('\"\"\"')) and flag:
                flag = False
                continue
            if (i.startswith('\'\'\'') or i.startswith('\"\"\"')) and not flag:
                flag = True
            count +=1
            countcode[0] += 1
    print(f"当前的文件路径为:{path},代码量为:{count}")
def main():
    countcode = [0]
    lujin = input(r"请输入你的文件路径:")
    count_path(lujin,countcode)
    print(f"总计代码行数为:{countcode[0]}")
main()

转载于:https://www.cnblogs.com/kuck/p/11366280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值