Did you look into using `with`? Is that available in this version of Python?