Skip to content

Instantly share code, notes, and snippets.

@greatwhole
Last active December 5, 2018 12:01
Show Gist options
  • Save greatwhole/f8aa60cc96c98309161666c157998e2c to your computer and use it in GitHub Desktop.
Save greatwhole/f8aa60cc96c98309161666c157998e2c to your computer and use it in GitHub Desktop.
requests_download
import os
import requests
RAISE = 1
DO_NOTHING = 2
RE_DOWNLOAD = 3
def download(url, path, action_on_exist=RAISE):
if os.path.exists(path):
if action_on_exist == RAISE:
raise Exception('file already exist | path=%s' % path)
elif action_on_exist == DO_NOTHING:
return
elif action_on_exist == RE_DOWNLOAD:
pass
else:
raise Exception('invalid arg <action_on_exist>')
r = requests.get(url, stream=True)
if r.status_code != 200:
raise Exception('invalid status_code {}'.format(r.status_code))
with open(path, 'wb') as f:
for chunk in r:
f.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment