I have 2 functions: one that reads a text file, and the other that changes that text file when a key is pressed. I need my code to print the text file every few seconds, and at the same time, monitor for key presses to change the file. Is this possible and how could I do it?
I've tried this, but it doesn't work.
def read_file():
color = open("color.txt", "r")
current_color = color.read()
color.close()
if current_color == "green":
print("GREEN")
elif current_color == "blue":
print("BLUE")
time.sleep(5)
def listen_change():
if keyboard.is_pressed('g'):
f = open("color.txt", "w")
f.write("green")
f.close()
elif keyboard.is_pressed('b'):
f = open("color.txt", "w")
f.write("blue")
f.close()
EDIT: here's how I tried multithreading
from threading import Thread
if __name__ == '__main__':
Thread(target=read_file()).start()
Thread(target=listen_change()).start()