requests save data to disk

Solutions on MaxInterview for requests save data to disk by the best coders in the world

showing results for - "requests save data to disk"
Aurélie
07 Jan 2017
1def download_file(url):
2    local_filename = url.split('/')[-1]
3    # NOTE the stream=True parameter below
4    with requests.get(url, stream=True) as r:
5        r.raise_for_status()
6        with open(local_filename, 'wb') as f:
7            for chunk in r.iter_content(chunk_size=8192): 
8                # If you have chunk encoded response uncomment if
9                # and set chunk_size parameter to None.
10                #if chunk: 
11                f.write(chunk)
12    return local_filename