document_util.py 864 B

12345678910111213141516171819202122232425262728293031
  1. import os
  2. from datetime import datetime
  3. class Singleton:
  4. _instance = None
  5. def __init__(self):
  6. if not Singleton._instance:
  7. print("__init__ method called..")
  8. else:
  9. print("Instance already created:", self.getInstance())
  10. @classmethod
  11. def getInstance(cls):
  12. if not cls._instance:
  13. cls._instance = Singleton()
  14. return cls._instance
  15. class TextFileWriter:
  16. def __init__(self, filepath):
  17. self.filepath = filepath
  18. if not os.path.exists(filepath):
  19. os.makedirs(filepath)
  20. def write_to_file(self, data):
  21. date_str = datetime.now().strftime('%Y-%m-%d')
  22. filename = f"{self.filepath}/{date_str}.txt"
  23. with open(filename, 'a') as file: # 'a' 模式可以在文件存在的情况下继续写入内容
  24. file.write(data + '\n')