Buckethead
Buckethead

Reputation: 135

Store chat messages in txt file

Let's assume that we have simple chat like THIS

have you any idea how to resolve problem with storing all messages in file since host is running, and when new client is connecting it see what other clients was writing before. When host is disconnected file is destroyed. I know that database use would be the easiest and quicest but I dont want to use any module or framework just pure python

Upvotes: 0

Views: 1588

Answers (1)

EngineersBox
EngineersBox

Reputation: 93

I think your best option is to initiate a new log file (.txt) at the beginning of each new chat. From there, write line by line the message with a time stamp to the .txt file.

To create a new .txt file in python:

f = open(“incremental_name.txt”, “x”)

Then when a message is sent, you write to that file:

f.open(“incremental_name.txt”, “a”)
f.write(“timestamp” + “user” + “message”)

When the chat is closed:

f.close()

Make sure for each new chat that the name of the log file is different, if you want seperate logs (best option). The “user” part in the message can be used to determine who it was that sent the message.

Upvotes: 3

Related Questions