# 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?