Thursday, September 20, 2012

Python!  Well its an easy language to learn.  I want to master it, so I'm making myself commit to at least three random that may or may not be useful, python tools a week.  This to keep my python skill sharp, and I will post my discoveries here.

The script grabs a url of unconditional file type to a file.

#!/usr/bin/python
# grabs a url to a file.

import sys
import urllib2
from os.path import basename
from urlparse import urlsplit

if len(sys.argv) < 2:
  print "Usage: %s <URL>" % sys.argv[0]
  sys.exit()

url = sys.argv[1]
urlContent = urllib2.urlopen(url).read()
fileName = basename(urlsplit(url)[2])
print "filename is: %s" % fileName
output = open(fileName,'wb')
output.write(urlContent)
output.close()

Trying to keep it simple as possible with minimal error checking.  You can add your own if you use the code.