Python 3 resuma a file download

Peter Otten __peter__ at web.de
Wed Jul 1 15:51:56 EDT 2015


zljubisic at gmail.com wrote:

> But how to read chunks?

Instead of

>         data = response.read() # a `bytes` object
>         out_file.write(data)
 
use a loop:

CHUNKSIZE = 16*1024 # for example
while True:
   data =  response.read(CHUNKSIZE)
   if not data:
       break
   out_file.write(data)


This can be simplified:

shutil.copyfileobj(response, out_file)

https://docs.python.org/dev/library/shutil.html#shutil.copyfileobj




More information about the Python-list mailing list