我正在使用python脚本控制树莓派上的一些GPIO引脚。 如下:
from gpiozero import PWMLED
import time
import os
from decimal import Decimal
led = PWMLED("GPIO12") #set the GPIO pin number you are using here. It uses the GPIO method of numbering.
#details on pin numbering can be found here: https://gpiozero.readthedocs.io/en/stable/recipes.html
setZero=open("/var/www/html/alpha.txt", "w")
setZero.write("0")
file.close()
while True: #a loop that never stops running
try: #prevents an bug with the code that would cause it to terminate when writing to alpha.txt file
initial=open("/var/www/html/alpha.txt", "r") #open the alpha.txt file as read only. Declare it as "initial"
brightness=(initial.read()) #declare the variable brightness by reading the variable "initial". Convert the string into a number
x = Decimal(brightness) #convert the number into a decimal. Store it as the variable X
int_brightness = round(x,2) #round the variable x to 2 decimal places. PHP should handle this, but just in-case. Store the rounded variable as int_brightness
led.value = int_brightness #set the brightness of the LED to the value of int_brightnesss
except:
() #return nothing if code fails
finally:
() #return nothing if code fails
问题出在以下几行:
setZero=open("/var/www/html/alpha.txt", "w")
setZero.write("0")
file.close()
如果我在没有这三行的情况下运行脚本,则代码可以正常工作。添加后,LED就会停止点亮(我假设while真循环未执行)。我已经确认启动时脚本会成功将0写入文件。
关于什么可能导致此操作失败的任何想法?控制台中没有返回的输出