Ensure file handle is closed using with statement

Ensure file handle is closed by using with statement.