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.

Recommended Answers

All 8 Replies

Run this piece of code and then take a look at where you can add some print statements to your code so you will know what is actually happening.

item = "third = 1"
if item.find("third"):
    print "item found at position", item.find("third")
else:
    print "item NOT found, value returned =", item.find("third")
#
if item.find("first"):
    print "item found at position", item.find("first")
else:
    print "item NOT found, value returned =", item.find("first")

You also want to take a look at the "startswith" string method as that is more in line with what you are doing.

I havn't checked the startswith function yet but did get some interesting results with this.

#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()
    print(newfile)
    print(mlist)
    for item in mlist:
        #if item found set x to item
        if item.find("third"):
            pass
        else:
            x = item[len(item) - 1]
    print(x)
    #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: "))

Results with configuration file set as "third = 1";

<io.TextIOWrapper object at 0x04484750>
['# 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']
1
1 setting is ok keep monitering?
Choice:

So newfile is set as some sort of object. And now the results if I set as "third = ":

Traceback (most recent call last):
  File "C:\programs\configexample.py", line 25, in <module>
    x = int(x)
ValueError: invalid literal for int() with base 10: '='

So what I'm seeing here is that it doesn't even print mlist or newfile after their creation near the begginging of the program. Does anyone know why this would be? Why does the code hang clear from that point until x = int(x), and only if 3 is taken out of the file? I guess when I have some time later tonight I'll give the startswith function a go but this seems puzzling.

Ok :) I'm officially a dinkus!!! I never even thought that I was bypassing it by having the other lines in the config file. Thank you for not spoiling all the fun though I learned alot more that way.

I jumped the gun to fast on that one even using the starts with only makes the if statement work correctly, the error still comes up if "third = ", and for some reason even a if x == "=": statement will not allow me to change the value of x to 0 so the code will run correctly. :(

I revised the code to look for explicitly "third = 1", and still the same problem. Even stranger if I set the config file with "third = 1" and run the program then change config file to "third =" it will print the list as it should appear to the program, but yet the return value remains the same.

#monitering enabled
mlist = []
choice = 0
while choice != 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()
    print(mlist)
    #if item found set x to item
    item = "third = 1"
    if item in mlist:
        print("if")
        x = item[len(item) - 1]
    else:
        x = 0
        print("else")
    
    print(x)
    #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?")
        choice = int(input("Choice: "))

Run code with "third = 1" output:

['# 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']
if
1
1 setting is ok keep monitering?
Choice:

Now change config file to "third =" save and chose to moniter again:

Choice: 1
['# 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 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 =']
if
1
1 setting is ok keep monitering?
Choice:

So for some reason it completly disregaurds the conditional statements. How can I work around this any ideas?

It works fine for me.

mlist = [ "first = 3", "second =", "third = 1"]
item_list = ["third = 1", "second = 2", "junk"]
for item in item_list:
    x = -1
    print "\ntesting", item
    if item in mlist:
        x = item[-1]
        print "if: x is now", x
    else:
        x = 0
        print("else")
    print x

x is being set to one in both of your examples. The only place that can come from is the file, so you changed the wrong file or something similiar. Post the output of the print mlist statement.

It runs correctly if I create the list inside the program such as your example, the problem comes when its actually checking the file. If I start the program with "third = " in the config file it doesn't an error is raised , however if I start the program with "third = 1", then while program is still running change the file to "third =" and save then have the program, it runs but x == 1.

Thanks for the help guys, it was a simple error on my part :(. I had some code running above the process I was testing and that's where the error was coming from. So I appologize for that but anyway here is the code and it's actually quite fun, I can see it coming in handy on some more complex projects.

#moniters configuration files for changes
mlist = []
choice = 0
while choice != 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()
    #if item found set x to item
    for item in mlist:
        if item.startswith("third"):
            x = item[len(item) - 1]
        else:
            x = 0
    if x == "=":
        x = 0
    #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 I'm happy!!")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.