diff --git a/boto/s3/key.py b/boto/s3/key.py index 99b7214c51..53bbe1af2f 100644 --- a/boto/s3/key.py +++ b/boto/s3/key.py @@ -1468,11 +1468,16 @@ def get_contents_to_filename(self, filename, headers=None, http://goo.gl/EWOPb for details. """ fp = open(filename, 'wb') - self.get_contents_to_file(fp, headers, cb, num_cb, torrent=torrent, - version_id=version_id, - res_download_handler=res_download_handler, - response_headers=response_headers) - fp.close() + try: + self.get_contents_to_file(fp, headers, cb, num_cb, torrent=torrent, + version_id=version_id, + res_download_handler=res_download_handler, + response_headers=response_headers) + except Exception: + os.remove(filename) + raise + finally: + fp.close() # if last_modified date was sent from s3, try to set file's timestamp if self.last_modified != None: try: