Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/12/2014, 14:10
startwav
 
Fecha de Ingreso: junio-2003
Mensajes: 122
Antigüedad: 20 años, 10 meses
Puntos: 0
Pregunta releer fichero en demonio phyton

hola,

estoy bastante pez con el tema del python pero me he visto en la necesidad de hacer un scripcillo para un programa q monitoriza unos ficheros.
el caso es que no se muy bien como hacer para que una vez el script en python está ejecutandose y que lee unos ficheros con un contenido numerico. cuando este fichero cambia, pq se actualiza cada 5min vuelva el demonio de script python a cargar el fichero abierto y use los nuevos valores.

No se si me he explicado bien..
os dejo el script para ver cómo lo véis y si me podéis decir cómo hacer para recargar estos ficheros que lee el script python:


----

Código Python:
Ver original
  1. #!/usr/bin/env /usr/bin/python
  2.  
  3. import subprocess
  4. import re
  5. import sys
  6. import time
  7. from datetime import datetime
  8. import httplib
  9.  
  10. import logging
  11. import logging.handlers
  12. import argparse
  13. import sys
  14. import time # this is only being used as part of the example
  15.  
  16. delay = 360
  17.  
  18. tempout = open("/home/pi/projects/tempo1.txt")
  19. tempout1 = tempout.read(8)
  20. tempout1 =float(tempout1)* 1.8 + 32
  21. tempout.close()
  22. #print tempout
  23.  
  24.  
  25.  # upload data to mi_web
  26.   try:
  27.         conn = httplib.HTTPConnection("miweb.com")
  28.         path = "/blablalba/blabalba?ID=xxxxxxxxx&PASSWORD=clave_pass&dateutc=" + str(datetime.utcnow().isoformat()) + "&humidity=" + str(humidity) + "&tempf=" + str(tempo
  29. ut1) + "&baromin=" + str(press1) + "&softwaretype=status=updateraw"
  30.         conn.request("GET", path)
  31.         res = conn.getresponse()
  32.     print path
  33.  
  34.  
  35.     # checks whether there was a successful connection (HTTP code 200 and content of page contains "success")
  36.         if ((int(res.status) == 200) & ("success" in res.read())):
  37.                 print "%s - Successful Upload\nTemp: %.1f F, Humidity: %.1f %%\nNext upload in %i seconds\n" % (str(datetime.now()), temp, humidity, delay)
  38.         else:
  39.                 print "%s -- Upload not successful, check username, password, and formating.. Will try again in %i seconds" % (str(datetime.now()), delay)
  40.   except IOError as e: #in case of any kind of socket error
  41.         print "{0} -- I/O error({1}): {2} will try again in {3} seconds".format(datetime.now(), e.errno, e.strerror, delay)
  42.  
  43.   # Wait before re-uploading data
  44.   time.sleep(delay)

-----

El fichero que necesito que se vuelva a leer en el demonio phyton de este script es tempo1.txt.. pero se queda abierto en el demonio y no hay forma que vuelva a cargarlo al pasar los 350segunds aprox.

alguna ayuda, please??

gracias!

Última edición por razpeitia; 04/12/2014 a las 21:05