Skip to content

Instantly share code, notes, and snippets.

@infinitylx
Created April 3, 2014 10:23
Show Gist options
  • Save infinitylx/9952048 to your computer and use it in GitHub Desktop.
Save infinitylx/9952048 to your computer and use it in GitHub Desktop.
Download files from file with list of urls.
import os
import urllib2, posixpath, urlparse
DOWNLOADS_DIR = './mp3'
# For every line in the file
for url in open('list.urls'):
resp = urllib2.urlopen(url)
# Split on the rightmost / and take everything on the right side of that
name = urllib2.unquote(posixpath.basename(urlparse.urlsplit(resp.url).path))
print('getting file ', name)
# Combine the name and the downloads directory to get the local filename
filename = os.path.join(DOWNLOADS_DIR, name)
# Download the file if it does not exist
if not os.path.isfile(filename):
with open(filename, 'wb') as fw:
fw.write(resp.read())
print('save file ', filename)
resp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment