python删除代码的单行注释,多行注释不删除,有备份到一个文件夹,源文件会覆盖

import os
import re


def keywords_in_line(keywords_to_keep,line):
    for keyword in keywords_to_keep:
        if  keyword in line:
            return True
    return False

from top.starp.util import file_util
from top.starp.util import time_util



def remove_comments_except_keywords(file_path, keywords_to_keep):
    """
    从文件中删除以'//'开头的注释行,但保留包含指定关键词和'http://'的行,以及多行注释块。

    Parameters:
        file_path (str): 文件路径。
        keywords_to_keep (list): 需要保留的关键词列表。

    """
    now_time_str=time_util.get_now_time_str()
    basename=file_util.basename(file_path)
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
    back_file_path=fr"D:/file\codeBack/{basename}_{now_time_str}"
    file_util.write_lines(back_file_path,lines)
    
    # 构建正则表达式模式
    keywords_pattern = '|'.join(rf'\b{re.escape(keyword)}\b' for keyword in keywords_to_keep)
    http_pattern = r'http://'
    pattern = rf'^(?!.*({keywords_pattern})).*//.*\n'

    filtered_lines = []
    in_comment_block = False

    for line in lines:
        if in_comment_block:
            filtered_lines.append(line)
            if '*/' in line:
                in_comment_block = False
        else:
            if '/*' in line:
                in_comment_block = True
                filtered_lines.append(line)
                if '/*' in line:
                    in_comment_block = False
                # 不是  http 或者  是可以 保存的
            elif re.search(http_pattern, line):
                filtered_lines.append(line)
            elif not line.strip().startswith("//"):
                filtered_lines.append(line)
            elif keywords_in_line(keywords_to_keep,line):
                filtered_lines.append(line)

    # 将修改后的内容写回文件
    with open(file_path, 'w', encoding='utf-8') as file:
        file.writelines(filtered_lines)


def traverse_directory(directory, keywords_to_keep):
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith('.txt') or file.endswith('.py') or file.endswith('.java'):
                file_path = os.path.join(root, file)
                remove_comments_except_keywords(file_path, keywords_to_keep)


def remove_comments_dir_or_file(file_or_dir_path):
    if os.path.isdir(file_or_dir_path):
        traverse_directory(file_or_dir_path,keywords_to_keep=keywords_to_keep)
    else:
        remove_comments_except_keywords(file_or_dir_path, keywords_to_keep=keywords_to_keep)

# str 
if __name__ == "__main__":


    keywords_to_keep = ['todo', 'TODO', 'tip', 'tips', '###',"note","NOTE"]  # 需要保留的关键词列表
    file_or_dir_path=rf"D:\file\shan\algorithm"
    remove_comments_dir_or_file(file_or_dir_path)
    # 多行注释不删除,有备份到一个文件夹,源文件会覆盖
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值