# Open new file to write file = None try: file = open(filePath, \'w\') except IOError: msg = (\"Unable to create file on disk.\") file.close() return
It is always advisable to write your logic or code that might throw an exception in the try block and use the finally block for closing the resources.