Python Sys.argv Example | Python argv, argv, argv
Python sys module provides access to any command-line arguments using the sys.argv method. It serves two purposes.
- The sys.argv is the list of all the command-line arguments.
- len(sys.argv) is the total number of length of command-line arguments.
Here sys.argv 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.
The arguments in the Python argv method are always parsed as the String.
So, you need to be careful if you are expecting your input to be of any other data type.
You may need to cast or convert the items according to your requirements.
#Storing command-line arguments
You can store any number of arguments given at the start of the program in the variables. Let’s see the following example.
# app.py import sys print('Number of arguments:', len(sys.argv), 'arguments.') print ('Argument List:', str(sys.argv)) data = sys.argv print('The argument three is:', data)
Now, run the file in the command line and see the output.
#Python argv and Python argv
For every invocation of Python, the sys.argv is automatically the list of strings representing the arguments (as separated by spaces) on the command-line.
The name comes from the C language convention in which the argv and argc represent the command line arguments.
But in the meantime, here are a few points to know.
You can create the script that prints the arguments as they’re represented. The command also prints the number of arguments, using the len function on the list.
See the following example.
#app.py from __future__ import print_function import sys print(sys.argv, len(sys.argv))
See the output.
➜ pyt python3 app.py ['app.py'] 1 ➜ pyt
If you call this script app.py, you can invoke it with different arguments to see what happens.
➜ pyt python3 app.py kl and kb ['app.py', 'kl', 'and', 'kb'] 4 ➜ pyt
As you can see, the command-line arguments include a script name but not the interpreter name.
In this sense, Python treats the script as the executable.
If you need to know the name of an executable (python in this case), you can use the sys.executable.
You can see from the examples that it is possible to receive arguments that do contain spaces if the user invoking the script with arguments encapsulated in quotes, so what you get is the list of arguments as supplied by the user.
Now in your Python code, you can use this list of strings as input to your program.
Since the lists are indexed by zero-based integers, you can get the individual items using the list syntax like sys.argv as filename and sys.argv, sys.argv are other parameters onwards.
For example, let’s get the script name.
#app.py from __future__ import print_function import sys script_name = sys.argv print(script_name)
See the output.
➜ pyt python3 app.py kl and kb app.py ➜ pyt
Python sys.argv contains the file name.
Python sys.argv contains the first command-line argument passed to your script.
Finally, Python Sys.argv Example | Python argv, argv, argv is over.