title: python counting funktion for text output in a loop
body: <p>I have a working code which tells me, when a laser and a photoresistor will be interrupted.</p>
  <p>It would be great, when I can write the information of time into a file and also a counting (i=i+1). So this will be done normally every 2 seconds. A writing after e.g. 500 breaks would be great, because it is running at a rasyperry pi. Less writing would be great.</p>
  <p>My output file should be something like this with unix time stamp. So I do not know where do I have to put my counter i = i + 1 at which position and the second question is about the caching issue.</p>
  <pre><code>12341234.12341234 1
  123478.234789 2
  12342134.12341234 3
  78989123.12341234 4
  </code></pre>
  <p>Code</p>
  <pre><code>import os, time
  from time import strftime,localtime
   
  RECEIVER_PIN = 23
   
  def callback_func(channel):
      if GPIO.input(channel):
          print(&quot;Sensor1: Lichtschranke wurde unterbrochen&quot;)
          date_local = strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, localtime())
          ticks = time.time()
          print(date_local)
          print(ticks)
          with open(&quot;/home/pi/Documents/sensors/sensor01_tageswert.txt&quot;,&quot;a+&quot;) as sensor01:
          #    sensor01.write(date_local)
              sensor01.write(str(ticks))
              sensor01.write(&quot;\n&quot;)
          # alternativ kann ein Script / Shell Befehl gestartet werden
          # os.system(&quot;ls&quot;)
   
  if __name__ == '__main__':
      
      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      
      GPIO.setup(RECEIVER_PIN, GPIO.IN)
      GPIO.add_event_detect(RECEIVER_PIN, GPIO.RISING, callback=callback_func, bouncetime=200)
          
      try:
          while True:
              time.sleep(0.25)
      except:
          # Event wieder entfernen mittels:
          GPIO.remove_event_detect(RECEIVER_PIN)
  
  </code></pre>