Definition
A memory-efficient pattern for downloading and saving large files by processing data in small, manageable pieces (chunks) rather than loading the entire file into RAM.
Why It Matters
In the age of Big Data, memory is the bottleneck. Iterative chunking is the “sip, don’t gulp” strategy for processing massive files, ensuring that your systems stay fast and stable even when handling data that exceeds their RAM.
Core Concepts
res.iter_content(chunk_size): A method that returns an iterator, yieldingchunk_sizebytes at a time.- Binary Write Mode: Use
open(filename, 'wb')to ensure the data is written exactly as received (crucial for images, PDFs, etc.). - Implementation Pattern:
res = requests.get(url) res.raise_for_status() with open('file.zip', 'wb') as f: for chunk in res.iter_content(100000): f.write(chunk)