Skip to content

Instantly share code, notes, and snippets.

@papachan
Created October 30, 2011 14:31
Show Gist options
  • Save papachan/1325961 to your computer and use it in GitHub Desktop.
Save papachan/1325961 to your computer and use it in GitHub Desktop.
python download url and save it to your compputer from a txt file
import os
import os.path
import time
import urllib2
import codecs
# python download url and save it to your compputer from a txt file
dirname = "/Users/papachan/Desktop/site/static/"
filename = "url.txt"
file = open(filename, 'r')
for url in file.readlines():
myfile = urllib2.urlopen(url)
list = url.split('/')[3:]
pathname = '/'.join(list)
filename = dirname + pathname
output = open(filename,'wb')
output.write( codecs.BOM_UTF8 )
output.write(myfile.read())
output.close()
if(os.path.exists(filename)):
print os.path.abspath(filename)
else:
print "Error file didnt save"
time.sleep(2)
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment