import threading
import time
def cold_temp():
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
tfile = open("/sys/bus/w1/devices/28-021571bf69ff/w1_slave")
# Read all of the text in the file.
text = tfile.read()
# Close the file now that the text has been read.
tfile.close()
# Split the text with new lines (\n) and select the second line.
secondline = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature/1000
return temperature
output = cold_temp()
f = open('/var/www/html/coldtemp.html', 'w')
print >> f, output
f.close()
cold_temp()
我已經試過以上和簡單Python中沒有運行每秒
def cold_temp():
while True:
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
tfile = open("/sys/bus/w1/devices/28-021571bf69ff/w1_slave")
# Read all of the text in the file.
text = tfile.read()
# Close the file now that the text has been read.
tfile.close()
# Split the text with new lines (\n) and select the second line.
secondline = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature/1000
return temperature
output = cold_temp()
f = open('/var/www/html/coldtemp.html', 'w')
print >> f, output
f.close()
time.sleep(1)
我想運行腳本的每一秒。上述兩個運行一次然後結束。
究竟是什麼問題? – TehSphinX
究竟發生了什麼?腳本崩潰了嗎?它每1.1秒運行一次還是不全? –
向我們展示'做點什麼'。 –