栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

获取 py文件代码行数(过滤有注释和空行的代码)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取 py文件代码行数(过滤有注释和空行的代码)

# -*- encoding=utf8 -*-
# Created Date: 2021/10/11
# @Author: "jiliqian"
import re, os
code_file_source = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lianxi1.py')
def get_lines():
    if '.py' in code_file_source:
        comment_line = 0
        blank_line = 0
        multi_comment_line = 0
        with open(code_file_source,encoding='utf-8', mode='r+') as f:
            lines = f.readlines()
            line_index = 0
            if len(lines) == 0:
                print("此.py文件代码内容为空")
            else:
                while line_index < len(lines):
                    line = lines[line_index]
                    if line.startswith("#"):
                        comment_line += 1  # 检查是否为#注释
                    elif line == 'n':
                        blank_line += 1  # 检查是否为空行
                    elif line.startswith("'''"):
                        multi_comment_line = +1 #检查是否为多行注释
                    line_index += 1
            lines_after_alter = int(len(lines)) - int(comment_line) - int(blank_line) - int(multi_comment_line)
            print(lines_after_alter)  # 输出python文件代码行数
    else:
        print("此文件夹下无.py文件")
if __name__=='__main__':
    get_lines()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/313790.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号