5,337
edits
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): | ||
if filename is None: | |||
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( | 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: | if chunk: | ||
f.write(chunk) | f.write(chunk) | ||
return full_path | |||
return | |||
</syntaxhighlight> | </syntaxhighlight> | ||