Python 统计某个文件夹/单个文件中的总行数,有效代码行数,注释行。

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

import os
import re

# 统计某个文件夹下的所有文件
directory_path = r"C:\Users\dir"

def get_line():
    total_lines = 0
    comment_lines = 0
    code_lines = 0

    # 支持的代码文件扩展名
    valid_extensions = {'.c', '.cpp', '.java', '.py', '.js', '.bin'}
    # 匹配以注释开头的行,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。
    single_line_comment = re.compile(r'//.*')
    multi_line_comment = re.compile(r'/\*.*?\*/', re.DOTALL)

    for root, _, files in os.walk(directory_path):
        for file_name in files:
            file_ext = os.path.splitext(file_name)[1].lower()           # 获取文件后缀
            if file_ext not in valid_extensions:
                continue  # 跳过非代码文件

            file_path = os.path.join(root, file_name)
            try:
                with open(file_path, 'r', encoding='utf-8') as file:
                    content = file.read()

                    # 统计多行注释
                    multi_comments = multi_line_comment.findall(content)
                    for comment in multi_comments:
                        comment_lines += comment.count('\n') + 1  # 计算多行注释的行数

                    # 移除多行注释以准确统计单行注释和代码行
                    content = multi_line_comment.sub('', content)
                    lines = content.split('\n')

                    for line in lines:
                        total_lines += 1
                        line = line.strip()
                        if not line:
                            continue  # 空行跳过

                        # 检查单行注释
                        if single_line_comment.match(line):
                            comment_lines += 1
                        else:
                            code_lines += 1

            except UnicodeDecodeError:
                print(f"警告: 文件 {file_path} 编码不兼容,已跳过")
                continue
            except Exception as e:
                print(f"错误: 处理 {file_path} 时发生异常 - {str(e)}")
                continue

    print(f"总行数: {total_lines}")
    print(f"注释行数: {comment_lines}")
    print(f"代码行数: {code_lines}")
    

get_line()
import os
import re

# 统计单个文件的总行,有效代码行,注释行。
file_path = r"C:\Users\dir\test.bin"        # 可自行修改

def get_file_line():
    total_lines = 0
    comment_lines = 0
    code_lines = 0
    single_line_comment = re.compile(r'//.*')
    multi_line_comment = re.compile(r'/\*.*?\*/', re.DOTALL)
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()

            # 统计多行注释
            multi_comments = multi_line_comment.findall(content)
            for comment in multi_comments:
                comment_lines += comment.count('\n') + 1  # 计算多行注释的行数

            # 移除多行注释以准确统计单行注释和代码行
            content = multi_line_comment.sub('', content)
            lines = content.split('\n')

            for line in lines:
                total_lines += 1
                line = line.strip()
                if not line:
                    continue  # 空行跳过

                # 检查单行注释
                if single_line_comment.match(line):
                    comment_lines += 1
                else:
                    code_lines += 1
    except UnicodeDecodeError:
        print(f"警告: 文件 {file_path} 编码不兼容,已跳过")


    print(f"总行数: {total_lines}")
    print(f"注释行数: {comment_lines}")
    print(f"代码行数: {code_lines}")


get_file_line()

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值