argv
What on Earth is it!?
Edit: If you can, will you please write a line or two and explain how it works?
The second parameter, argv (argument vector), is an array of pointers to arrays of character objects. The array objects are null-terminated strings, representing the arguments that were entered on the command line when the program was started.
The argv argument is a vector of C strings; its elements are the individual command line argument strings. The file name of the program being run is also included in the vector as the first element; the value of argc counts this element.
argv is the list of commandline arguments passed to the Python program. argv represents all the items that come along via the command line input, it's basically an array holding the command line arguments of our program. Don't forget that the counting starts at zero (0) not one (1).
argv(ARGument Vector) is array of character pointers listing all the arguments. If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will contain pointers to strings. Argv[0] is the name of the program , After that till argv[argc-1] every element is command -line arguments.
Script printArgv.py:
import sys
print sys.argv.__class__
spam = sys.argv[1:]
print spam
Run it with:
python printArgv.py 1 2 3 4 5
You would find the output:
<type 'list'>
['1', '2', '3', '4', '5']
Which means the sys.argv is a list of parameters you input following the script name.
Try this simple program, name it as program.py
import sys
print sys.argv
and try executing
python program.py
python program.py a b c
python program.py hello world
Note what is argv now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With