Created
April 3, 2014 10:23
-
-
Save infinitylx/9952048 to your computer and use it in GitHub Desktop.
Download files from file with list of urls.
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 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