Ensure file is closed promptly in case of error
Fixes #35566.  Replaces #35567.
Not quite sure how the test failure is going to work in this case:  previous issues (e.g. #13932) mention ResourceWarnings but I don't have time to set up a full local build environment to duplicate the tests. @jreback do you have any insight from a few years back? copied the milestone from #35567. feel free to amend as appropriate. Hello @rxxg! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:









There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2020-09-05 11:01:11 UTC Added the What's New note.
I also added a try/except to ensure correctness under more circumstances, but it raised two PEP8 flags (which I silenced).  Happy to discuss my justification. /azp run <samp>
Azure Pipelines successfully started running 1 pipeline(s).<br>

</samp> I'm fine with this implementation, but curious: wouldnt the idiomatic way to do this be with a contextmanager?