import os import re import codecs def create_menu_structure(current_page): menu = f'''

地产后台管理

''' return menu def update_html_files(): # 获取所有HTML文件 html_files = [f for f in os.listdir('.') if f.endswith('.html')] for file_name in html_files: try: print(f"正在处理: {file_name}") # 读取文件内容 with codecs.open(file_name, 'r', 'utf-8') as file: content = file.read() # 创建新的菜单HTML new_menu = create_menu_structure(file_name) # 定义匹配模式 menu_pattern = r'.*?\s*\s*\s*\s*' # 查找所有匹配项 matches = list(re.finditer(menu_pattern, content, re.DOTALL)) if matches: # 从最后一个匹配项开始替换,以避免位置偏移 for match in reversed(matches): start, end = match.span() content = content[:start] + new_menu + content[end:] # 保存更新后的内容 with codecs.open(file_name, 'w', 'utf-8') as file: file.write(content) print(f"已完成: {file_name}") else: print(f"警告: {file_name} 中没有找到菜单") except Exception as e: print(f"处理 {file_name} 时出错: {str(e)}") continue def update_menu_in_file(file_path): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() # 查找新房服务菜单部分 pattern = r'(.*?
)(.*?)(<\/div>\s*<\/div>)' # 新的菜单项HTML new_menu_item = ''' 轮播管理 ''' def replace_menu(match): menu_start = match.group(1) menu_items = match.group(2) menu_end = match.group(3) # 检查是否已经存在轮播管理菜单项 if '轮播管理' not in menu_items: # 在视频管理之前插入新菜单项 menu_items = menu_items.replace( '