Use with statement to close file handle

This change ensures the file handle is closed automatically (similar to the use of the `with` statement a few lines above it).