Ensure file handle is closed by using with statement

This change ensures the file handle is closed (similar to the README code in `setup.py` below it).