python - 使用 Python 3(而非 Python 2)下载时 Zip 文件损坏

标签 python python-2.7 python-3.x

我正在尝试使用 Python3 下载 zip 文件,但每次文件都已损坏。

我认为以下代码在 Python2 中运行良好,但现在似乎无法在 Python 3 中运行 - P2 和 P3 之间是否存在可能导致 shutil 工作方式出现问题的变化?

    zip_file = requests.get(zip_package_url, headers = request_headers, stream=True)
    with open(zip_file_name, 'wb') as out_file:
        shutil.copyfileobj(zip_file.raw, out_file)

最佳答案

已解决 - 从请求中获取字节格式,然后保存为字节似乎可行:

    zip_file = requests.get(zip_package_url, headers = request_headers, stream=True).content
    with open(zip_file_name, 'wb') as out_file:
        out_file.write(zip_file)

关于python - 使用 Python 3(而非 Python 2)下载时 Zip 文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37126265/

相关文章:

python - 如何在 python 中使用变量作为文件名的一部分打开文件?

python - 编译Python代码时出现unindent does not match any external indenting level错误

python 3 - 使用负数 ndigits 舍入 Decimal 导致使用科学计数法的 Decimal

python正则表达式根据标准分割

Python:高效调用多个返回函数的子集变量

python - Pandas 信息未显示所有列和数据类型

Python:如何获取方括号内的多个元素

python - 我如何检查理解的时间复杂度

python-3.x - 使用boto3批量上传json到dynamodb

python - 在 BeautifulSoup 中替换文本而不转义