python try:except:finally

user354051 picture user354051 · Oct 15, 2011 · Viewed 92.5k times · Source
# Open new file to write
file = None
try:
    file = open(filePath, 'w')
except IOError:
    msg = ("Unable to create file on disk.")
    file.close()
    return
finally:
    file.write("Hello World!")
    file.close()

The above code is ripped from a function. One of the user's system is reporting an error in line:

file.write("Hello World!")

error:

AttributeError: 'NoneType' object has no attribute 'write'

Question is, If python is failed to open given file, 'except' block executes and it has to return, but control is getting transferred to the line that is throwing given error. The value of 'file' variable is 'None'.

Any pointers?

Answer

Acorn picture Acorn · Oct 15, 2011

You shouldn't be writing to the file in the finally block as any exceptions raised there will not be caught by the except block.

The except block executes if there is an exception raised by the try block. The finally block always executes whatever happens.

Also, there shouldn't be any need for initializing the file variable to none.

The use of return in the except block will not skip the finally block. By its very nature it cannot be skipped, that's why you want to put your "clean-up" code in there (i.e. closing files).

So, if you want to use try:except:finally, you should be doing something like this:

try:
    f = open("file", "w")
    try:
        f.write('Hello World!')
    finally:
        f.close()
except IOError:
    print 'oops!'

A much cleaner way of doing this is using the with statement:

try:
    with open("output", "w") as outfile:
        outfile.write('Hello World')
except IOError:
    print 'oops!'