Perhaps this should be a [context manager](https://docs.python.org/2/reference/datamodel.html#context-managers) instead?