Hello, I'm working on a new little project that moniters a configuration file. Everything seems to work great except for when I attempt to do some error handling. The configuration file simply looks like this.
# third sets the lights
# 0 = off, 1 = on, 3 = lights burnt out
# please leave the lights on unless they are burnt out
first = 1
secound = 2
third = 1
Third is the only line being monitered as you can see from the code.
#monitering enabled
mlist = []
while x != 9:
x = 0
#open config file to look for setting
newfile = open("confige.txt", "rt")
for line in newfile:
mlist.append(line.strip())
newfile.close()
for item in mlist:
#if item found set x to item
if item.find("third"):
pass
else:
x = item[len(item) - 1]
#if x still 0 someone trying to shut the lights off
#lights are to be left on
x = int(x)
if x == 0:
print("I said keep it on!")
else:
x = repr(x)
print(x + " setting is ok keep monitering?")
x = int(input("Choice: "))
Ok works great untill the configuration file is changed to something like; third =. When that happens third is still located so it goes to.
else:
x = item[len(item) - 1]
Fine and dandy, x is now "=" right. Well I tried several ways to handle this, such as.
else:
x = item[len(item) - 1]
print(x)
if x == "=" or x == "":
x = 0
else:
pass
#if x still 0 someone trying to shut the lights off
#lights are to be left on
x = int(x)
I was certain the solution should be as simple as this. I also tried an try/except clause, with no success. As far as I can tell the biggest problem is that the code makes a jump from x = item[len(item) - 1] to the conditional statement. In this case even print(x) is bypassed and never executes. I also had a few other question regarding this little program so I will list them all in this post.
1. Is this problem normal and is there a way to fix it, or a better way to code it.
2. I'm considering this program to be a process. I'm also assuming that for another "process" to moniter the same configuration file is not possible withought making a copy of the configuration file information. Would the best way to allow a seprate "process" to moniter the same file be to have this process copy its information to a seprate file and let the other process moniter that file? Or to somehow pass this information from this program itself?
Hopefully question 2 makes some sence to somone :). If not I'll post an updated version later on if anyone is interested or if I run into any problems with it. Thanks on any information ahead of time.