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') as file: # 'a' 模式可以在文件存在的情况下继续写入内容 file.write(data + '\n')