Skip to content

Instantly share code, notes, and snippets.

@somada141
Last active December 31, 2016 09:36
Show Gist options
  • Save somada141/217c172839079f66c1de to your computer and use it in GitHub Desktop.
Save somada141/217c172839079f66c1de to your computer and use it in GitHub Desktop.
Download files with Python and the requests package #python #fileIO #web #requests
import requests
def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
return local_filename
# source
# http://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py
@hnwfs
Copy link

hnwfs commented Dec 31, 2016

commented out: f.flush()
makes the code slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment