加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 10:58:38 所属栏目:Python 来源:网络整理
导读:bug:当遇到3个“”“时 可能会将下面的代码不计入代码总行数 import osdef 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': coun

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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读