COMMAND-LINE ARGUMENTS
Python provides a getopt module to parse command-line options and arguments, and the Python sys module provides access to any command-line arguments via the sys.argv. This serves two purposes:
* sys.argv is the list of command-line arguments * len(sys.argv) is the number of command-line arguments
Here sys.argv[0] is the program name, so if the Python program is called test.py, it matches the value of sys.argv[0].
Now you can provide input values for a Python program on the command line instead of providing input values by prompting users for their input.
As an example, consider the script test.py shown here:
#!/usr/bin/python
import sys
print('Number of arguments:',len(sys.argv),'arguments')
print('Argument List:', str(sys.argv))
Now run above script as follows:
python test.py arg1 arg2 arg3
This will produce following result:
Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2&apos...