Open In App

Python script to open a Google Map location on clipboard

Last Updated : 04 Nov, 2019
Like Article

The task is to create a python script that would open the default web browser to the Google map of the address given as the command line argument.

Following is the step by step process: 

  1. Creating the Address_string from command line input : Command line arguments can be read through sys module. The sys.argv array has the first element as the filename and the rest of elements as command line arguments which are broken into different elements by spaces, same as raw_input().split(). Therefore, if the length of sys.argv is more than 1, then we can be sure that the command line arguments have been passed.
    Since sys.argv is a list of strings, it can be passed to join() method which returns a single string value. Since the first element is the filename, which is not needed, we can slice the list and join from 2nd element onwards.

    #File name is
    import sys
    print ' '.join(sys.argv[1:])


    If we run >>> python New Delhi
    The output of the program would be New Delhi.
  2. Open the Web-Browser : We will be using web browser module for opening the browser. The webbrowser module has a method open() that can launch the web browser to the specified URL. For example, the script given below will open the web browser to the home page of GeeksforGeeks.

    import webbrowser'')


  3. Find the URL : Now, when we go to Google Maps and search for google maps, the URL turns out to be gibberish and of no clear pattern, as shown below.,77.4077907,17z/data=!3m1!4b1!4m5!3m4!1s0x390ce626851f7009:0x621185133cfd1ad1!8m2!3d28.5011226!4d77.4099794?hl=en

    Websites often add extra text in the URL for additional tasks like customization and tracking. However, it can be observed that the initial few part of URL is where GeeksforGeeks is our searched keyword.
    Also, for example say while searching for New Delhi, instead of New + Delhi, if we write only New Delhi, the + is inserted in the required places on its own, which eases our task even further.
    Therefore, the final URL can be taken as

  4. Combining the two and Completing the script : The python script to open the given command line address is given below. There will be two imported modules, webbrowser to open the browser to the designated URL and sys to work on the command line arguments.
    • The first step is to check if there is any command line given or not, which is done using the len(sys.argv).
    • Then we use the join method to form the address string of the place that is to be searched in Google Maps.
    • Finally, when we get the address, we open the browser to the address URL using the open() method of webbrowser module.

    The program is run through CMD(windows) or terminal(Linux) in the following format:

    >>> python [File Name] [Address to be searched]
    For eg. >>> python GeeksforGeeks

    # File Name --
    import sys, webbrowser
    if len(sys.argv) > 1:       # Argument passed
        map_string = ' '.join(sys.argv[1:])'' + map_string)
        print "Pass the string as command line argument, Try Again"


    >>> python SeeksforGeeks
    The above command will open map of GeeksforGeeks in the web browser.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads