download files from server in python using stream code example
Example 1: requests save data to disk
def download_file(url):
local_filename = url.split('/')[-1]
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
return local_filename
Example 2: how to redirect where requests library downloads file python
import requests
file_url = 'https://www.journaldev.com/wp-content/uploads/2019/08/Python-Tutorial.png'
file_object = requests.get(file_url)
with open('Python-Tutorial.png', 'wb') as local_file:
local_file.write(file_object.content)