mercredi 11 juillet 2018

Return value as bool from text file

Trying to write multi scripts that would access one text file to retrieve their toggle values from the said text file and then return with a value as false and change it to true and vice versa.

Value always comes back as false, not sure what I'm missing but here is the text file:

1
False
3
True
5
6
7
8
9

Here is the source code:

def append():
with open('values', 'r') as file:
    # read a list of lines into data
    data = file.readlines()
# now change the line
re_value = value

data[1] = re_value+"\n"
# and write everything back
with open('values', 'w') as file:
    file.writelines(data)
print("value changed to "+re_value)

def read() -> bool:
#opens the value file to find the value of the toggle
f=open("values", "r")

for x, line in enumerate(f):
    if x == 1:
        toggle = line
        print(toggle)
        return toggle
f.close()


toggle = read()

if toggle:
    print("Light is on turn it off")
    #runs command to turn off the light
    #runs command to change value for light
    value = "False"
    append()
else:
    print("Light is off turn it on")
    #runs command to turn on the light
    #runs command to change value for light
    value = "True"
    append()

Aucun commentaire:

Enregistrer un commentaire