import logging import os from pathlib import Path def write_log(file_name, info): project_root = Path(__file__).parent.parent # 拼接 demo/demo.xlsx 路径 file_path = project_root / "log" / (file_name+".txt") logging.basicConfig(filename=file_path, level=logging.INFO, force=True) # 将 file 的内容写入日志 logging.info(info) # 关闭日志系统,确保日志写入文件 logging.shutdown() def clear_log(file_name): project_root = Path(__file__).parent.parent # 拼接 demo/demo.xlsx 路径 file_path = project_root / "log" / (file_name+".txt") with open(file_path, "w") as f: pass # 直接写入空内容