11/06/2016

Python : python call file python , While, Time Delay

python : python call file python

Code : Call other file
  1. import os
  2. os.system('filename.py')

Example :
  1. os.system('/home/pi/checkair.py')


Code : Time Delay
  1. import time
  2. while True:
  3.     print "This prints once a minute."
  4.     time.sleep(60)  # Delay for 1 minute (60 seconds)


Example : All
  1. #!/usr/bin/python
  2. #### Check temperature Adafruit_DHT 22 23 #######
  3. import sys
  4. import os
  5. import Adafruit_DHT
  6. import time
  7.  
  8. humidity, temperature = Adafruit_DHT.read_retry(22, 23)
  9.  
  10. #while(humidity is None and temperature is None):
  11. while(temperature is None):
  12.         humidity, temperature = Adafruit_DHT.read_retry(22, 23)
  13.         time.sleep(10)  # Delay for 10 seconds before loop.
  14.  
  15. #### On Or Off Relay By Piface  #####
  16. import pifacedigitalio
  17. pf = pifacedigitalio.PiFaceDigital()
  18. import smtplib
  19.  
  20. print 'Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity)
  21. if temperature > 27:
  22.         pf.relays[1].turn_on()
  23.         pf.relays[0].turn_on()
  24.  
  25.         from smtplib import SMTP
  26.  
  27.         recipients = ['suwit.jph@gmail.com', '"komkid@gmail.com', 'nontiwaza@gmail.com']
  28.         def send_email (message, status):
  29.                 fromaddr = 'scivalve.suwit@gmail.com'
  30.                 server = SMTP('smtp.gmail.com:587')
  31.                 server.ehlo()
  32.                 server.starttls()
  33.                 server.ehlo()
  34.                 server.login('scivalve.suwit@gmail.com', 'PASSWORD')
  35.                 server.sendmail(fromaddr, recipients, 'Subject: %s\r\n%s' % (status, message))
  36.                 server.quit()
  37.  
  38.         send_email("High temp alert! in Server Room A. " '{0:0.1f}*C'.format(temperature), "High temp alert! Turn ON relays.")
  39. else:
  40.         pf.relays[0].turn_off()
  41.         pf.relays[1].turn_off()
  42.  
  43. pf.deinit_board()
  44.  

http://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another

No comments:

Post a Comment