Prevent exceptions from being masked in get_contents_to_filename

If opening the file fails, there is the potential for the following call to `os.remove` to fail, suppressing the actual error.