document_util.py 925 B

123456789101112131415161718192021222324252627282930313233
  1. import json
  2. import os
  3. from datetime import datetime
  4. class Singleton:
  5. _instance = None
  6. def __init__(self):
  7. if not Singleton._instance:
  8. print("__init__ method called..")
  9. else:
  10. print("Instance already created:", self.getInstance())
  11. @classmethod
  12. def getInstance(cls):
  13. if not cls._instance:
  14. cls._instance = Singleton()
  15. return cls._instance
  16. class TextFileWriter:
  17. def __init__(self, filepath):
  18. self.filepath = filepath
  19. if not os.path.exists(filepath):
  20. os.makedirs(filepath)
  21. def write_to_file(self, data):
  22. date_str = datetime.now().strftime('%Y-%m-%d')
  23. filename = f"{self.filepath}/{date_str}.txt"
  24. with open(filename, 'a', encoding='utf-8') as file: # 指定编码为 'utf-8'
  25. json.dump(data, file, ensure_ascii=False) # 禁用 ASCII 转义
  26. file.write('\n')