file.write останавливается при выполнении истинного цикла в python3 - PullRequest
0 голосов
/ 25 марта 2019

Я использую скрипт Python для управления некоторыми выводами GPIO на Raspberry Pi. Выглядит следующим образом:

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()

Если я запускаю скрипт без этих 3 строк, код работает нормально. Как только я его добавлю, светодиод перестанет светиться (я полагаю, что пока истинный цикл не выполняется). Я подтвердил, что при запуске скрипт успешно записывает 0 в файл.

Любые идеи о том, что может быть причиной этого провала? В консоли нет возвращенного вывода

...