Python: close files properly to avoid a ResourceWarning on Python 3.x