I am trying to download a zip file with Python 3 requests:
try:
r = requests.get(BASEURL, stream=True)
with open(localfiletitle, 'wb') as fd:
shutil.copyfileobj(r.raw, fd)
except requests.exceptions.RequestException as e:
send_notice_mail("Error downloading the file:", e)
return False
but I am always getting a partial, invalid file. I have also tried using urllib.request
, and in that case I am getting http.client.IncompleteRead: IncompleteRead
error.
You need to extract the value from content
, not raw
with open(localfiletitle, 'wb') as fd:
shutil.copyfileobj(io.BytesIO(r.content), fd)
You can also work with it without saving it using ZipFile
data = io.BytesIO(r.content)
with ZipFile(data) as zip_file:
...