Ensuring that a file is closed