Stream a Large Download to Disk

Hard ⏱ 18 min 39% acceptance ★★★★★ 4.9
Downloading a 2 GB file via response.content loads it all into RAM. Write download(url, dest) using stream=True and iter_content(chunk_size=8192), writing chunks to dest in binary mode and returning the byte count written.

Examples

Example 1
Input
download(url, 'backup.zip')
Output
total bytes written, file on disk
Explanation

Chunks flow straight from socket to file.

Constraints

  • stream=True is mandatory.
  • Write in 'wb' mode.

Topics

APIsstreaming

Companies

DropboxGoogle Drive TeamAWS

Hints

Hint 1

for chunk in response.iter_content(8192):.

Hint 2

Skip empty keep-alive chunks (if chunk:).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.