Python: Difference between revisions

26 bytes added ,  26 February 2020
Line 213: Line 213:
[https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests SO Answer]
[https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests SO Answer]
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
def download_file(url):
def download_file(url, folder=None, filename=None):
     local_filename = url.split('/')[-1]
     if filename is None:
     # NOTE the stream=True parameter below
        filename = path.basename(url)
    if folder is None:
        folder = os.getcwd()
     full_path = path.join(folder, filename)
     with requests.get(url, stream=True) as r:
     with requests.get(url, stream=True) as r:
         r.raise_for_status()
         r.raise_for_status()
         with open(local_filename, 'wb') as f:
         with open(full_path, 'wb') as f:
             for chunk in r.iter_content(chunk_size=8192):  
             for chunk in r.iter_content(chunk_size=8192):
                 if chunk: # filter out keep-alive new chunks
                 if chunk:
                     f.write(chunk)
                     f.write(chunk)
                    # f.flush()
     return full_path
     return local_filename
</syntaxhighlight>
</syntaxhighlight>