Last active
December 5, 2018 12:01
-
-
Save greatwhole/f8aa60cc96c98309161666c157998e2c to your computer and use it in GitHub Desktop.
requests_download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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