Here’s a step-by-step process:
- Create Address_string from the command line: command line arguments can be read through the sys module. The sys.argv array has the first element as the filename and the remaining elements as command line arguments, which are separated into different elements by spaces, like raw_input (). Split (). Therefore, if the length of sys.argv is greater than 1, we can be sure that the command line arguments are passed.
Since sys.argv is a list of strings, it can be passed to the join () method, which returns a single string value. Since the first element — this is the filename, which is not needed, we can slice the list and join from the second element onwards.# File name - Map.py
import
sys
print
’’
. join (sys.argv [
1
:])
If we run""" python Map.py New Delhi The output of the program would be New Delhi.
- Open your web browser: we will use the web browser module to open the browser. The web browser plug-in has a open () method that can launch a web browser at the specified URL. For example, the script below will open a web browser to the Python.Engineering home page.
import
webbrowser
webbrowser.
open
(
’ https://python.engineering/ ’
)
- Search URL. Now when we go to Google Maps and search for Google maps, the URL turns out to be unreasonable and lacks a clear pattern as shown below.
https://www.google.co.in/maps/place/GeeksforGeeks/@28.5011226,77.4077907,17z/data=!3m1!4b1!4m5! 3m4! 1s0x390ce626851f7009: 0x621185133cfd1ad1! 8m2! 3d28.5011226! 4d77.4099794? gl = enWebsites often add extra text to the URL for additional tasks such as customization and tracking. However, you may notice that the initial portion of the URL — this is https://www.google.co.in/maps/place/GeeksforGeeks, where Python.Engineering is our search keyword.
Also, for example, say, when looking for New Delhi instead of New Delhi, if we write only New Delhi, the + sign is inserted in the right places on its own, which makes our task even easier.
Therefore, the final URL can be taken as https://www.google.co.in/maps/place/ Address_String / . - Combining the two and ending the script: A python script to open the given command line address is given below. There will be two imported modules, a web browser to open the browser at the specified URL and sys to work with command line arguments.
- Step One & # 8212 ; check if any command line is set or not, which is done with len (sys.argv).
- We then use the join method to form the address bar of the place to look for in Google Maps.
- Finally, when we get the address, we open the browser to the URL using the open () method of the webbrowser module.
The program is launched via CMD (windows) or terminal ( Linux) in the following format:
""" python [File Name] [Address to be searched] For eg. & gt;"" python Map.py Python.Engineering
# File name - Map.py
import
sys, webbrowser
if
len
(sys.argv)"
1
:
# Argument passed
map_string
=
’ ’
. join (sys.argv [
1
:])
webbrowser.
open
(
’ https://www.google.com/maps/place/ ’
+
map_string)
else
:
print
"Pass the string as command line argument, Try Aga in "
""" python Map.py SeeksforGeeks The above command will open map of Python.Engineering in the web browser.
This article courtesy of Harshit Agrawal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.