Python practice: open web pages from command-line using webbrowser and sys.argv modules

#! usr/bin/env python3
# by lorenzo - 03/10/2017
# open web pages from command-line using webbrowser and sys.argv modules
import webbrowser, sys

if len(sys.argv) > 1:
    for i in range(1,len(sys.argv)):
        if sys.argv[i].startswith("www.") or sys.argv[i].startswith("http://"):
            if sys.argv[i].startswith("http://"):
                webbrowser.open(sys.argv[i])
            else:
                webbrowser.open("http://" + sys.argv[i]) # webbrowser.open() needs URLs to start with "http://"

Comments about the code are welcome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s