I want to ensure that all resources are being cleaned correctly. Is this a safe thing to do:
try:
closing(open(okFilePath, "w"))
except Exception, exception:
logger.error(exception)
raise
EDIT:
Infact, thinking about it, do I even need the try/catch as I am raising the exception anyways I can log at a higher level. If it errors on creating the file, one can assume there is nothing to close?
To be sure that the file is closed in any case, you can use the with statement. For example:
try:
with open(path_to_file, "w+") as f:
# Do whatever with f
except:
# log exception
you can use this to just create a file and close it in one line.
with open(file_path, 'w') as document: pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With