Ensure file handle is closed by using with statement

This ensures the file handle is automatically closed.