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)
# 多行注释不删除,有备份到一个文件夹,源文件会覆盖
python删除代码的单行注释,多行注释不删除,有备份到一个文件夹,源文件会覆盖
最新推荐文章于 2024-11-09 21:51:27 发布