Ensure file handle is closed using with statement (core.py)