def save_html_to_file(html_content, file_name): import os # 保存 HTML 文件的路径,假设保存到 /home 目录下 file_path = f"/home/aihtml" directory = os.path.dirname(file_path) if not os.path.exists(directory): os.makedirs(directory) rs_name = f"{file_path}/{file_name}.html" try: # 打开文件并写入 HTML 内容 with open(rs_name, "w", encoding="utf-8") as file: file.write(html_content) print(f"HTML 文件已成功保存到 {rs_name}") except Exception as e: print(f"保存文件时出现错误: {e}") return f"{file_name}.html" if __name__ == "__main__": # 示例 HTML 内容 text = "Hello, World!" html_content = f""" 保存的文字内容

{text}

""" # 示例文件名称 file_name = "4213" # 调用方法保存 HTML 文件 save_html_to_file(html_content, file_name)