Created
April 6, 2014 11:26
-
-
Save drmalex07/10004729 to your computer and use it in GitHub Desktop.
A download helper for Python scripts. #python #download
This file contains 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 sys | |
import os.path | |
import urllib2 | |
def download_file(url, dest_file): | |
try: | |
ifp = urllib2.urlopen(url, data=None) | |
with open(dest_file, 'wb') as ofp: | |
n = 0 | |
s = ifp.read() | |
while s: | |
n += len(s) | |
ofp.write(s) | |
s = ifp.read() | |
print '[info] Fetched %d bytes from %s into %s' %(n, url, dest_file) | |
ofp.close() | |
ifp.close() | |
except Exception as ex: | |
print '[error] Failed to fetch %s: %s' %(url, str(ex)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment