Thank you for your donation!

LCD script
I have the following python script to post data from lcd.txt to a 4x20 LCD.

import time
from RPLCD.i2c import CharLCD

lcd = CharLCD('PCF8574', 0x27)
lcd.backlight_enabled = (1)
lcd.cursor_pos = (0, 0)

mylines = []                                       # Declare an empty list.
with open ('/home/pi/lcd.txt', 'rt') as myfile:    # Open file for reading text.
   myfile = myfile.readlines()[1:4]               # Only read lines 1 to 4.
   for myline in myfile:                          # For each line in the file.
       mylines.append(myline.rstrip('\n'))        # strip newline and add to list.
for element in mylines:                            # For each element in the list.
   element=element[element.find('=')+1:]          # Only print text after = sign.
#    print(element)


I am completely new to python and want to monitor the file lcd.txt to see if it has changed and then run again the above script to update the LCD.  Can anyone help I have tried lots of ideas I found online but cannot make it work.  Below is one that will monitor the file but what do i need to add to the above to do the monitoring and trigger the update?

def read_file():
   with open("/home/pi/lcd.txt", "r") as f:
       SMRF1 = f.readlines()
   return SMRF1

initial = read_file()
while True:
   current = read_file()
   if initial != current:
       for line in current:
           if line not in initial:
       initial = current


Forum Jump: