Recuperare parametri da command line (command line arguments)
Updated at: 22/11/2013


The Python sys module provides access to any command-line arguments via the sys.argv. This serves two purpose:
  • 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 ie. script name.

Example:

Consider the following script test.py:
#!/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', 'arg3']
NOTE: As mentioned above, first argument is always script name and it is also being counted in number of arguments.