3

I'm working on a project with python in a raspberry pi.
I have a log file that is continuously logging time and at a specific second a command is sent to it (a Shed command from a water heater.)

I'm trying to write a code that reads the last line in that log file and turn off GPIO X when a shed command is sent (and does nothing when last line is just a time-stamp).

Here is an algorithm to make it clear:
- Read log file
- if last line is shed command: turn off GPIO X
----else: do nothing

I tried one method but it does not read the last line, it reads every line and goes back to the first when it reaches the last line. I only want to read the last line.

Here is a code I tried:
import time 
fileHandle = open ('UCM.log') 
lineList = fileHandle.readlines() 
fileHandle.close() 

while True: 
    #for line in (open(UCM.log).readlines()): 
    if " Sending Application message: Shed" in lineList[len(lineList)-1]: 
        time.sleep 
        print 'Shed!'
stovfl
  • 14,998
  • 7
  • 24
  • 51
  • 3
    Post the code that you've tried. It's easier for people to debug than to write you an entirely new program. – Darkstarone May 19 '17 at 01:17
  • Here is a code I tried:import time fileHandle = open ('UCM.log') lineList = fileHandle.readlines() fileHandle.close() while True: #for line in (open(UCM.log).readlines()): if " Sending Application message: Shed" in lineList[len(lineList)-1]: time.sleep print 'Shed!' – Motlaq AlMutairi May 19 '17 at 05:28
  • You can edit your own questions and add code in code blocks for easier editing. As it stands, no one can use your code because we have no idea what your indentation is. – Darkstarone May 19 '17 at 07:42
  • Possible duplicate of [How can I tail a log file in Python?](http://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python) – stovfl May 19 '17 at 09:05

1 Answers1

0

Apparently your problem is to read a file from the end. You could check the answers to this questions: Read a file in reverse order using python

Community
  • 1
  • 1
user1620443
  • 784
  • 3
  • 14
  • 1
    I'm not trying to read it in reverse. I'm trying to read the last line only. and if the line is:" Sending Application message: Shed", i want to turn off a GPIO. – Motlaq AlMutairi May 19 '17 at 05:27