0

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()

1 Answers1

0

The target argument has to be a function. You're calling the functions immediately, not passing references to the function.

The functions need to loop.

Use join() to wait for the threads to exit.

Use a lock to prevent reading and writing the file at the same time, which might read the file in a partial state.

import keyboard
from threading import Thread, Lock

mutex = Lock()

def read_file():
   while True:
       with mutex:
           with open("color.txt", "r") as color:
               current_color = color.read()
       if current_color == "green":
           print("GREEN")
       elif current_color == "blue":
           print("BLUE")
       time.sleep(5)

def listen_change():
   while True:
       if keyboard.is_pressed('g'):
           with mutex:
               with open("color.txt", "w"):
                   f.write("green")
       elif keyboard.is_pressed('b'):
           with mutex:
               with open("color.txt", "w"):
                   f.write("blue")

if __name__ == '__main__':
    t1 = Thread(target = read_file)
    t2 = Thread(target = listen_change)
    t1.start()
    t2.start()
    t1.join() # Don't exit while threads are running
    t2.join()
Barmar
  • 741,623
  • 53
  • 500
  • 612