Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Created April 6, 2014 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmalex07/10004729 to your computer and use it in GitHub Desktop.
Save drmalex07/10004729 to your computer and use it in GitHub Desktop.
A download helper for Python scripts. #python #download
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