Command Line Arguments in Python

Python 9 min min read Updated: Mar 09, 2026 Intermediate
Command Line Arguments in Python
Intermediate Topic 10 of 10

Python Command Line Arguments

Command line arguments allow users to pass input values to a Python program directly from the terminal while executing the script. This feature is extremely useful when building command-line tools, automation scripts, or data processing programs where input needs to be provided dynamically.

Instead of hardcoding values inside a program, command line arguments allow the user to provide data during execution.

What are Command Line Arguments?

Command line arguments are parameters passed to a Python script when it is executed from the command line. These arguments can be accessed within the Python program and used for processing tasks such as file handling, configuration settings, or data processing.

Example command:

bash python script.py John 25

Here, John and 25 are command line arguments passed to the script.

Using sys.argv

Python provides the sys module to access command line arguments using sys.argv.

The argv list contains the following elements:

  • argv[0] – Name of the script
  • argv[1] – First argument
  • argv[2] – Second argument
python import sys print("Script name:", sys.argv[0]) print("First argument:", sys.argv[1]) print("Second argument:", sys.argv[2])

Running the program:

bash python script.py Alice 30

Output:

Script name: script.py
First argument: Alice
Second argument: 30

Handling Multiple Arguments

You can process multiple arguments using loops.

python import sys for arg in sys.argv: print(arg)

This will print all arguments passed to the script.

Converting Arguments to Numbers

Command line arguments are received as strings, so they must be converted if numeric operations are required.

python import sys num1 = int(sys.argv[1]) num2 = int(sys.argv[2]) print("Sum:", num1 + num2)

Execution:

bash python script.py 10 20

This program calculates the sum of two numbers provided through the command line.

Checking Number of Arguments

To avoid errors, it is good practice to check whether the required number of arguments is provided.

python import sys if len(sys.argv) < 3: print("Please provide two numbers") else: num1 = int(sys.argv[1]) num2 = int(sys.argv[2]) print("Sum:", num1 + num2)

This ensures that the program does not crash due to missing arguments.

Using argparse Module

For more advanced command-line tools, Python provides the argparse module. It allows defining arguments, help messages, and default values.

python import argparse parser = argparse.ArgumentParser(description="Simple calculator") parser.add_argument("num1", type=int) parser.add_argument("num2", type=int) args = parser.parse_args() print("Sum:", args.num1 + args.num2)

This approach provides better argument handling and automatic help messages.

Displaying Help Message

The argparse module automatically generates help documentation.

bash python script.py --help

This command displays usage instructions for the script.

Real-World Example

Command line arguments are commonly used in automation scripts such as file processors.

python import sys filename = sys.argv[1] with open(filename, "r") as file: print(file.read())

This program reads a file whose name is provided through the command line.

Best Practices for Command Line Arguments

  • Always validate user input.
  • Use argparse for complex command-line tools.
  • Provide helpful error messages.
  • Document script usage clearly.

Conclusion

Command line arguments allow Python programs to receive input directly from the terminal, making scripts more flexible and interactive. By using modules like sys and argparse, developers can build powerful command-line applications for automation and data processing.

Understanding command line arguments is an essential step toward building professional Python tools and utilities.

In the next tutorial, we will explore Working with APIs in Python and learn how Python communicates with web services.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators