Argument is URL or path

xralf picture xralf · Oct 21, 2011 · Viewed 9.5k times · Source

What is the standard practice in Python when I have a command-line application taking one argument which is

URL to a web page

or

path to a HTML file somewhere on disk

(only one)

is sufficient the code?

if "http://" in sys.argv[1]:
  print "URL"
else:
  print "path to file"

Answer

locojay picture locojay · Mar 30, 2013
import urlparse

def is_url(url):
    return urlparse.urlparse(url).scheme != ""
is_url(sys.argv[1])