January 2019
Beginner to intermediate
776 pages
19h 58m
English
Sometimes it is necessary to pass arguments to the script from the command line. This is generally needed when we need to perform some quick actions in our script, rather than the script asking us for the inputs.
Consider the following lines of code where we pass two numbers as arguments to scripts, and print the sum of them:
import sysprint ("Total output is ")print (int(sys.argv[1])+int(sys.argv[2]))
When we run this script, say it's saved as checkargs.py, and execute it as follows:
python checkargs.py 5 6
The output returned is as follows:
Total output is11
The key here is the import of the sys module, which is a predefined module in Python to handle any system-related tasks of Python. The values ...
Read now
Unlock full access