Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "argv", and what does it do?

Tags:

python

argv

What on Earth is it!?

Edit: If you can, will you please write a line or two and explain how it works?

like image 339
Dylan Richards Avatar asked Nov 07 '12 05:11

Dylan Richards


People also ask

What does argv do?

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.

What is * argv [] in C?

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.

How does argv work in Python?

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).

What does argv have?

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.


2 Answers

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.

like image 146
R.Z Avatar answered Oct 02 '22 14:10

R.Z


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.

like image 24
Senthil Kumaran Avatar answered Oct 02 '22 14:10

Senthil Kumaran