Python getting last line
<p>Working on a monitoring project, I need to count pulses from pulse meters. I've already found some solutions, which I've been trying to adapt to my needs. </p>

<p>Here is the python script, running on a Raspberry Pi :</p>

<pre><code>#!/usr/bin/env python

import RPi.GPIO as GPIO
import datetime
import sys
import signal
from subprocess import check_output

#verbose = True     # global variable

############################################################################################################
############################################################################################################

def printusage(progname):
        print progname + ' &lt;gpio-pin-number&gt; &lt;filename&gt; [debug]'
        print 'Example usage: ' 
    print progname + ' 23 /path/to/mylogfile'
        print progname + ' 23 /path/to/mylogfile debug'
    sys.exit(-1)

def signal_handler(signal, frame):
        if verbose:
        print('You pressed Ctrl+C, so exiting')
    GPIO.cleanup()
        sys.exit(0)


def readvalue(myworkfile):
    try:
        f = open(myworkfile, 'ab+')     # open for reading. If it does not exist, create it
        line=subprocess.check_output(['tail','-f','-1',f])
        elmts=line.split(" ")
        value = int(elmts[2])   
    except:
        value = 0               # if something went wrong, reset to 0
    #print "old value is", value
    f.close()   # close for reading
    return value


def writevalue(myworkfile,value):
    f = open(myworkfile, 'a')
    f.write((str(datetime.datetime.now())+' '+str(value)+'\r\n'))   # timestamp
    f.close()   

############################################################################################################
############################################################################################################

######### Initialization


#### get input parameters:

try:
    mygpiopin = int(sys.argv[1])
    logfile = sys.argv[2]
except:
    printusage(sys.argv[0])

verbose = False
try:
    if sys.argv[3] == 'debug':
        verbose = True
        print "Verbose is On"
    else:
        printusage(sys.argv[0])
except:
    pass

#### if verbose, print some info to stdout

if verbose:
    print "GPIO is " + str(mygpiopin)
    print "Logfile is " + logfile
    print "Current value is " + str(readvalue(logfile))


#### setup

GPIO.setmode(GPIO.BCM)
GPIO.setup(mygpiopin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

signal.signal(signal.SIGINT, signal_handler)    # SIGINT = interrupt by CTRL-C


########## Main Loop 

while True:
    # wait for pin going up
    GPIO.wait_for_edge(mygpiopin, GPIO.RISING)

    # read value from file
    counter=readvalue(logfile) + 1
    if verbose:
        print "New value is", counter

    # write value to file
    writevalue(logfile,counter)

    # and wait for pin going down
    GPIO.wait_for_edge(mygpiopin, GPIO.FALLING)

############################################################################################################
############################################################################################################
</code></pre>

<p>I want to get the last index registered, and increment it, but everything I've tested so far leaves the loop stuck on an index of 1. </p>

<p>I can't use a "heavier" method to find the last line, for instance browsing the entire file, because it will only get heavier and heavier as time passes, and I can't miss a pulse. </p>

<p>I'm pretty new to programming, so thanks for your help !</p>

<p>EDIT : the result file looks like this : </p>

<pre><code>2016-10-08 16:54:23.072469 1
2016-10-08 16:54:23.462465 1
2016-10-08 16:54:23.777977 1
2016-10-08 16:54:24.010045 1
2016-10-08 16:54:24.194032 1
2016-10-08 16:54:24.388120 1
2016-10-08 16:54:24.549389 1
2016-10-08 16:54:24.737994 1
2016-10-08 16:54:24.959462 1
2016-10-08 16:54:25.164638 1
2016-10-08 16:54:25.351850 1
2016-10-08 16:54:25.536655 1
2016-10-08 16:54:25.716214 1
2016-10-08 16:54:25.794152 1
2016-10-08 17:06:13.506531 1
2016-10-08 17:06:14.097642 1
2016-10-08 17:06:14.211579 1
2016-10-08 17:06:15.237852 1
2016-10-08 17:06:15.752239 1
2016-10-08 17:06:16.320419 1
2016-10-08 17:06:16.842906 1
2016-10-08 17:06:17.391121 1
2016-10-08 17:06:17.851521 1
2016-10-08 17:06:18.444486 1
2016-10-08 17:06:18.858358 1
</code></pre>

<p>Your logic is way over complicated, if you will be running the script different times just store the last index in a separate file constantly overwriting:</p>

<pre><code> def next_index(index):
    with open(index, 'a+') as f:  # open for reading. If it does not exist, create it
        val = int(next(f, -1)) + 1 # first run value will be 0
    open(index,"w").write(str(val)) # overwrite
    return val
</code></pre>

<p>if the value is created elsewhere just do the writing there, just make sure you overwrite the previous value opening with <code>w</code>. </p>

python search raspberry-pi pulse