Skip to content

Instantly share code, notes, and snippets.

@IrSent
Created November 22, 2016 06:20
Show Gist options
  • Save IrSent/37f469a96b7a075e2baa7456fe64ae0d to your computer and use it in GitHub Desktop.
Save IrSent/37f469a96b7a075e2baa7456fe64ae0d to your computer and use it in GitHub Desktop.
Python File Download # Ways of handling file download using Requests library
# Ways of handling file download using Requests library
import contextlib
import os
import requests
def download_file(url, dest_dir):
# reassurance that the connection will be closed:
# Requests cannot release the connection back to the pool unless you consume all the data or call Response.close
with contextlib.closing(requests.get(url=url, stream=True)) as response:
# Make sure there is a folder to save a file
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
local_file_path = os.path.join(dest_dir, url.split('/')[-1])
if not response.ok:
return None
else:
with open(local_file_path, 'wb') as f:
# customize the chunk size depending on files you are going to download
for chunk in response.iter_content(chunk_size=128):
if chunk:
f.write(chunk)
return local_file_path
# the only difference here is that now we use
# shutil library to handle file save process
import os
import shutil
import requests
def download_file(url, dest_dir):
with contextlib.closing(requests.get(url=url, stream=True)) as response:
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
local_file_path = os.path.join(dest_dir, url.split('/')[-1])
if response.ok:
with open(local_file_path, 'wb') as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment