write_log.py 681 B

12345678910111213141516171819202122232425
  1. import logging
  2. import os
  3. from pathlib import Path
  4. def write_log(file_name, info):
  5. project_root = Path(__file__).parent.parent
  6. # 拼接 demo/demo.xlsx 路径
  7. file_path = project_root / "log" / (file_name+".txt")
  8. logging.basicConfig(filename=file_path, level=logging.INFO, force=True)
  9. # 将 file 的内容写入日志
  10. logging.info(info)
  11. # 关闭日志系统,确保日志写入文件
  12. logging.shutdown()
  13. def clear_log(file_name):
  14. project_root = Path(__file__).parent.parent
  15. # 拼接 demo/demo.xlsx 路径
  16. file_path = project_root / "log" / (file_name+".txt")
  17. with open(file_path, "w") as f:
  18. pass # 直接写入空内容