Argument Handling in Python
Argument handling gives user the chance to interact with the script and use it for different tasks. Two ways to do this in Python are with ARGV and optparse. The optparse module offers more options for argument handling, but we will use the sys module for our simple example to demonstrate argument handling in Python programming language.
So far we have created simple scripts with a static nature, now it is time to create Python scripts that interact with the user. We need the sys module to use ARGV structure which is a list containing the name of the program (the name of the script in our case) and all arguments passed by the user in the command-line. How does the user know what arguments to pass in the command-line? We will use the sys.exit() function to inform the user about the program usage if the user does not have any information about its usage. We will build a simple calculator that finds the sum of two numbers passed by the user in the command-line. Open a new terminal and use your favorite text editor to create a Python file with the name ‘calculator.py’ like shown in Figure 1.
After adding the shebang line it is the time to import necessary modules for our program.
The sys module will help us to handle user arguments passed in the command-line, so we import this important module.
Our program will calculate the sum of two numbers so the sys.argv list will contain three elements, the name of the program and the two numbers that will be passed by the user in the command-line. We will use an if conditional to check if the length of the sys.argv list is equal to three and if not, we will tell the user how to use the program.
We use the sys.exit() function to exit the system and inform the user about the program usage. If we run the program now without any argument the output will be the same with the output in Figure 5.
Until now, our program works pretty well. We will use sys.argv to get the user input from the command-line. The first number is sys.argv and the second number is sys.argv. The sys.argv element of the list is the name of the program, in our case it is calculator.py. We use two variables, num1 and num2 like shown in Figure 6.
After linking numbers to variables the heavy work is done, we add the two numbers together and print the sum on the screen. Figure 8 shows how our programs works.
I hope you find very helpful in learning Argument Handling in Python this simple example.
Like us on Facebook
We need your assistance to stay live
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- News from Kaspersky Lab: Tor is protecting criminals : In a previous article I wrote a bit about Tor. Free browser, relays bouncing your IP all over the ...1 comment |
- Grive: An Unofficial, Open Source Linux Client For Google Drive : A couple of months before, we have featured a Linux client for Google Drive named Syncdrive, which w...2 comments |
- How To Access A LVM Drive / Partition in Ubuntu : Hey Guys, I installed Fedora on an external drive (LVM) recently and was trying to access it from U...0 comments |
- Adding and Deleting Cluster Resources (corosync pacemaker) : In our previous tutorial, we have shown you how to install corosync and pacemaker. We will show y...0 comments |
- Install Satis On Ubuntu Sever 13.10/13.04 : Satis is a simple static composer repository generator. It is used in any composer.json file as i...0 comments |
- Adding and Deleting Cluster Resources (corosync pacemaker)
- News from Kaspersky Lab: Tor is protecting criminals
- Install Satis On Ubuntu Sever 13.10/13.04
- How To Access A LVM Drive / Partition in Ubuntu
- How To Disable Remember Open Files in Sublime Text 3
- Install Sublime Text 3 In Fedora Easily With Fedy
- Grive: An Unofficial, Open Source Linux Client For Google Drive
- Install nSnake Game In Terminal
- How To “Fix E: Could not open file /var/lib/dpkg/status”
- Wow! Linux Foundation To Offer Free Linux Course This Summer Worth $2,400 on EdX
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .