try:
f = open(path)
print(fh.read())
except:
print('No such file '+path
finally:
f.close()
Inside complex code parts (for example multiple loops, complex data constructions…), avoid using try…catch…finally.
When an exception is thrown, a variable (the exception itself) is created in a catch block, and it’s destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.
try:
f = open(path)
print(fh.read())
except:
print('No such file '+path
finally:
f.close()
if os.path.isfile(path):
fh = open(path, 'r')
print(fh.read())
fh.close