python:提取下载文件时意外结束文件异常

时间:2018-01-18 08:27:10

标签: python python-requests gzip

我正在尝试使用python从Amazon S3下载一些文件并将其解压缩到本地磁盘。

以下是我用来下载和解压缩文件的代码。

def download(self):
    filename = BASE_OUTPUT_DIR + "/" + str(self.jobId) + "_" + string.replace(self.jobName, " ", "_") + "_" + str(self.count)
    try:
        response = requests.get(self.fileUrl.rstrip(), stream=True, verify=False)
        if response.status_code == 200:
            with open(filename + ".gz", 'wb') as out_file:
                shutil.copyfileobj(response.raw, out_file)
            del response
            subprocess.call(["gunzip", "--name", filename])
            return filename
    except Exception as e:
        self.logger.debug("An exception occurred while downloading file (%s)" % (filename))
        self.logger.debug("Trying to delete the file")
        try:
            os.system('rm -rf %s' % filename)
        except Exception as e:
            self.logger.debug(e)
    return None

有时我收到此错误gzip: /tmp/files/file_8340.gz: unexpected end of file

我怀疑文件,但这个问题随机文件随机发生。我认为下载没有完成,但我不确定。

0 个答案:

没有答案
相关问题