Seems like this could be an if statement instead of a while loop. You also seem to be opening the file twice (line 34), undoing any error catching you do here.