Ensure file handle is closed by using with statement

This change ensures the file handle is closed (similar to README below it).