self.filename=os.path.join(r"C:\Workspace\Appli\Logs",f"{datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}_{self.username}_chatlog.txt")# set the filename
self.write_to_file()# call the function to write the first line to the file
defwrite_to_file(self):
withopen(self.filename,"a")asfile:
timestamp=datetime.datetime.now().strftime("%Y-%m-%d%H:%M:%S")# get the current time in the desired format
file.write(f"{timestamp}{self.chat_log[-1]}\n")# write the timestamp and the latest chat log entry to the file