Properly use try-finally for file handle management
Fix pylint error
Merge pull request #12982 from basepi/safeclose10658

Properly use try-finally for file handle management