Download large file in python with requests
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Large downloads fail for different reasons than small ones. The goal is not only to fetch the bytes, but to do it without loading the whole response into memory, while still handling timeouts, partial files, and HTTP errors cleanly.
Stream the Response Instead of Loading It All
The most important setting in requests is stream=True. Without it, requests may download the full body before your code starts writing to disk.
This writes the file in one-megabyte chunks and keeps memory usage low even for very large responses.
Write to a Temporary File First
A robust downloader should not leave a half-finished target file behind if the request fails. Writing to a temporary file and renaming it only after success is a safer pattern.
That way, the final path contains either a complete file or no file at all.
Add Basic Progress Reporting
If the server sends Content-Length, you can show progress while writing.
This is optional, but it is very useful for long transfers.
Resuming an Interrupted Download
If the server supports range requests, you can resume from the last written byte instead of starting over.
If the server ignores the Range header and returns the full file with status 200, you should discard the partial file and restart instead of appending blindly.
Handle Errors Deliberately
Large downloads are more exposed to network interruptions, permission problems, and short server outages. Use raise_for_status() for HTTP errors and wrap the request in try and except when the calling code needs retry or cleanup behavior.
It also helps to set both connect and read timeouts, as shown above with timeout=(5, 30). That avoids hanging forever on a stalled connection.
Common Pitfalls
- Omitting
stream=Truecan cause unnecessary memory use on large files. - Writing directly to the final filename risks leaving behind a corrupted partial file.
- Appending to an existing file without checking whether the server honored the
Rangeheader can corrupt the download. - Using very tiny chunk sizes increases overhead, while extremely large chunks can reduce responsiveness.
- Ignoring timeouts and status checks makes failures harder to diagnose.
Summary
- Use
stream=Trueanditer_content(...)for large downloads. - Write to disk incrementally instead of loading the full response into memory.
- Prefer a temporary file and rename-on-success pattern.
- Support resuming only when the server actually honors range requests.
- Combine chunked writing with timeouts and status checks for a reliable downloader.
Related reading
- Downloading a picture via urllib and python
- Drop all duplicate rows across multiple columns in Python Pandas
- Drop columns whose name contains a specific string from pandas DataFrame
- Drop data frame columns by name
- Dropping infinite values from dataframes in pandas?
- Dump a NumPy array into a csv file
- Dump a NumPy array into a csv file
- Duplicate log output when using Python logging module
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.