Python sys module provides access to any command-line arguments using the sys.argv method. It serves two purposes.
Here sys.argv[0] is the program, i.e. script name. If you are going to work with command-line arguments, you probably want to use sys.argv.
Let’s take the example of sys.argv command and see the output of the following program.
## app.py
import sys
print('Number of arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))
Now, go to your command-line tool and type the following command with the arguments following by space and hit the enter and see the output.
Now, analyze the output. We have passed the six arguments, and we got six arguments in the Argument List.
As mentioned above, our first argument is always the script name, and it is also being counted in the number of arguments. So even if you do not pass any arguments to your script, the argv variable always contains at least one element, and that is the script name.
#python #python sys.argv #sys.argv method