| 123456789101112131415161718192021222324252627282930313233 |
- import json
- import os
- from datetime import datetime
- class Singleton:
- _instance = None
- def __init__(self):
- if not Singleton._instance:
- print("__init__ method called..")
- else:
- print("Instance already created:", self.getInstance())
- @classmethod
- def getInstance(cls):
- if not cls._instance:
- cls._instance = Singleton()
- return cls._instance
- class TextFileWriter:
- def __init__(self, filepath):
- self.filepath = filepath
- if not os.path.exists(filepath):
- os.makedirs(filepath)
- def write_to_file(self, data):
- date_str = datetime.now().strftime('%Y-%m-%d')
- filename = f"{self.filepath}/{date_str}.txt"
- with open(filename, 'a', encoding='utf-8') as file: # 指定编码为 'utf-8'
- json.dump(data, file, ensure_ascii=False) # 禁用 ASCII 转义
- file.write('\n')
|