Answers for "download files from server in python using stream"

0

requests save data to disk

def download_file(url):
    local_filename = url.split('/')[-1]
    # NOTE the stream=True parameter below
    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): 
                # If you have chunk encoded response uncomment if
                # and set chunk_size parameter to None.
                #if chunk: 
                f.write(chunk)
    return local_filename
Posted by: Guest on August-19-2020
0

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)

#The file will be downloaded in the same directory as the Python script.
#If you want to change the directory location,
#you can provide a complete path or relative path in the open() function call.
Posted by: Guest on July-08-2020

Code answers related to "download files from server in python using stream"

Python Answers by Framework

Browse Popular Code Answers by Language