Latest Code Tutorials

Python sys.argv: How to use sys.argv in Python

The sys module provides access to some variables used or maintained by the interpreter and functions that interact strongly with the interpreter. Python sys module provides access to any command-line arguments using the sys.argv method.

If you work with command-line arguments, you probably want to use sys.argv.

Python sys.argv

The sys.argv is a built-in Python command that list all the command-line arguments. The len(sys.argv) is the total number of length of command-line arguments. The sys.argv[0] is the program, i.e. script name.

Let’s take the example of the sys.argv command and see the output of the following program.


import sys

print('Number of arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))

Now, go to your command-line tool, type the following command with the arguments followed by space, hit the enter, and see the output.

Python Sys.argv Tutorial

Now, analyze the output. We passed the six arguments and got six arguments in the Argument List.

As mentioned above, our first argument is always the script name, which counts 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, which is the script name.

The arguments in the Python argv method are always parsed as the String.

You must be careful if you expect 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.


import sys

print('Number of arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))

data = sys.argv[2]
print('The argument three is:', data)

Now, run the file in the command line and see the output.

Storing command line arguments

Python argv[0] and Python argv[1]

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.

You can learn more about lists and strings in this blog.

But in the meantime, here are a few points to know.

You can create a 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.

from __future__ import print_function
import sys
print(sys.argv, len(sys.argv))

See the output.

➜  pyt python3
[''] 1
➜  pyt

If you call this script, you can invoke it with different arguments to see what happens.

➜  pyt python3 kl and kb
['', '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 invokes 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 to input your program.

Since zero-based integers index the lists, you can get the individual items using the list[0] syntax like sys.argv[0] as filename and sys.argv[1], sys.argv[2] are other parameters onwards.

For example, let’s get the script name.

from __future__ import print_function
import sys

script_name = sys.argv[0]

See the output.

➜  pyt python3 kl and kb
➜  pyt

Python sys.argv[0] contains the file name.

Python sys.argv[1] contains the first command-line argument passed to your script.

That’s it for this tutorial.

Recommended Posts

Python removes duplicates from the list

Python list extend()

Python pop()

Python dir()

Python sum()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.