Python 3 resuma a file download

Cameron Simpson cs at zip.com.au
Thu Jul 2 00:50:10 CEST 2015


On 01Jul2015 21:51, Peter Otten <__peter__ at web.de> wrote:
>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)

It might be worth noting here that Peter's "chunk" is just an arbitrary amount 
of data to read in a unit. I'm only mentioning this because the HTTP 
specification has a "chunked-encoding" used to return binary files, and we are 
not talking about _those_ chunks.

Cheers,
Cameron Simpson <cs at zip.com.au>

Life is uncertain.  Eat dessert first.  - Jim Blandy


More information about the Python-list mailing list