Skip to content

Instantly share code, notes, and snippets.

@ykshatroff
Created January 18, 2020 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykshatroff/44ca2ced418543bc6e079b2007572cc8 to your computer and use it in GitHub Desktop.
Save ykshatroff/44ca2ced418543bc6e079b2007572cc8 to your computer and use it in GitHub Desktop.
from datetime import datetime
# get current date
now = datetime.now()
curtimestamp = now.strftime("%Y%m%d-%H:%M:%S")
def read_line(line: str) -> tuple:
"""
Line format:
2020-01-18 09:00:00, message
"""
date, text = line.split(',', 1)
return datetime.fromisoformat(date), text
def read_from_file(file_name) -> tuple:
buffer = ""
# read the text file with our message log
buffer += 'Reading messages.txt\n'
parsed_content = []
try:
with open(file_name) as file:
for line in file:
parsed_content.append(read_line(line))
except FileNotFoundError:
buffer += 'File not found\n'
return buffer, parsed_content
def user_input():
return input('Please enter a new message (type quit to exit): ')
def write_to_file(toAdd):
with open('messages.txt', 'a') as f:
f.write(f'{curtimestamp}, {toAdd}\n')
if __name__ == '__main__':
buffer, parsed = read_from_file('messages.txt')
print(buffer)
while True:
# ask for new input
toAdd = input('Please enter a new message (type quit to exit): ')
if toAdd == "quit":
print("Bye!")
break
# string to write
if toAdd:
with open('messages.txt', 'a') as f:
f.write(f'{curtimestamp}, {toAdd}\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment