Python with statement issue

Viewed 72

New to python and found one box still running very old code that doesn't support the with statement and admins not interested in upgrading as it will be replaced, but no ETA on replacement and need to get my script working

On another box with later version script works fine, but need to get it working on this older box

Extract from working script on later version

with open("ping.log","r") as reader:
    while True:
        line = reader.readline()
        if len(line)==0:
            break

        status = line[34]
        name = line[16:30]
        if (status=="d"):
            html+= '<tr>\n<td>\n<font color="red">'+ name+'</font><br>\n</td>\n</tr>\n'
        else:
            html+= '<tr>\n<td>\n<font color="green">'+ name+'</font><br>\n</td></tr>\n'

It basically opens file reads the contents and looks at specific position to see if device is up or down and make it red or green

Now I know you can use something like

fileh = open(file, 'w')
try:
    # Do things with fileh here  
finally:
    fileh.close()

Need help with the part between try and finally

2 Answers

You were actually on the right path:

fileh = open("ping.log", 'r') # you don't need the as reader part
try:
    for line in fileh:            # you can just iterate over the file, no need for while True
        if len(line)==0:
            break
        status = line[34]
        name = line[16:30]
        if (status == "d"):
            html+= '<tr>\n<td>\n<font color="red">'+ name+'</font><br>\n</td>\n</tr>\n'
        else:
            html+= '<tr>\n<td>\n<font color="green">'+ name+'</font><br>\n</td></tr>\n'
except:
    print("ERROR")
finally:
    fileh.close()

You could even make a variable color and set it depending on your status, so the lines are easier to change later like:

...
    color = "red" if (status == "d") else "green"
    html+= '<tr>\n<td>\n<font color="' + color + '">'+ name + '</font><br>\n</td>\n</tr>\n'
...

EDIT: As suggested, I updated for try, except, finally

EDIT 2: For your second problem, as I understand it, you want to write or append to the file dash.aspx?

Then you wouldn't have to iterate over it

...
writer = open("dash.apsx", "w") #You need to check whether you want to use 'a' or 'w' for appending or writing
#no need to iterate over writerlines, you already have the file and can directly write to it

#INFO: this will put everything on one line, for new lines you could add "\n" e.g. writer.write(htmlBeg + "\n") etc.
writer.write(htmlBeg) 
writer.write("<tr>") 
writer.write(htmlTBeg) 
writer.write('<th style="text-align:left"><h3>APAC Region Firewalls</h3></th>') 
writer.write(html) 
writer.write(htmlTEnd)
#close file at the end
write.close()
...

The as fileh part needs to be changed to an assignment to the file handle variable you want to use, reader, and that basically fixes it.

To wit,

reader = open("ping.log", 'r')  # not 'w' if you are reading
try:
    # Do things with reader here
    # Basically copy-paste the stuff which was inside the with statement
finally:
    reader.close()
Related