Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip files corrupted when downloaded using Python 3 (not Python 2)

I am trying to download zip files using Python3, however each time the file is corrupted.

I think the following code worked fine in Python2, however now does not seem to work Python 3 - has there been a change between P2 and P3 that is likely to cause issues with how shutil works?

    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)
like image 492
kyrenia Avatar asked Feb 16 '26 08:02

kyrenia


1 Answers

Solved - getting in byte format from requests, and then saving as bytes seems to work:

    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)
like image 159
kyrenia Avatar answered Feb 19 '26 00:02

kyrenia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!