from datetime import datetime def save_html_to_file(html_content, file_name): import os # 保存 HTML 文件的路径,假设保存到 /home 目录下 current_time = datetime.now().strftime("%Y%m%d%H%M%S") file_path = f"/home/aihtml/{file_name}_{current_time}.html" directory = os.path.dirname(file_path) if not os.path.exists(directory): os.makedirs(directory) try: # 打开文件并写入 HTML 内容 with open(file_path, "w", encoding="utf-8") as file: file.write(html_content) print(f"HTML 文件已成功保存到 {file_path}") except Exception as e: print(f"保存文件时出现错误: {e}") return f"{file_name}_{current_time}.html" if __name__ == "__main__": # 示例 HTML 内容 text = "Hello, World!" html_content = f"""
{text}
""" # 示例文件名称 file_name = "4213" # 调用方法保存 HTML 文件 save_html_to_file(html_content, file_name)