Python tutorial explains it:

import sys

print(sys.argv)

More specifically, if you run python example.py one two three:

>>> import sys
>>> print(sys.argv)
['example.py', 'one', 'two', 'three']
Answer from SilentGhost on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › command-line-arguments-in-python
Command Line Arguments in Python - GeeksforGeeks
In Python, command line arguments are values passed to a script when running it from the terminal or command prompt.
Published   December 17, 2025
People also ask

Can I access command-line arguments directly in Python?
You can access command-line arguments directly using the sys.argv list. sys.argv contains all the arguments passed to the script, with sys.argv[0] being the script name and subsequent elements representing the passed arguments.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › understanding command line arguments in python
Command Line Arguments in Python: Examples & Tips
How do I handle optional arguments in Python?
You can use the argparse module to handle optional arguments in Python. Define optional arguments using the add_argument method, specifying the -- prefix. For example, parser.add_argument('--output', help=' Output file') handles an optional output file argument.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › understanding command line arguments in python
Command Line Arguments in Python: Examples & Tips
How do I handle positional arguments in Python?
Positional arguments in Python can be handled using the argparse module. Define them without the -- prefix in the add_argument method. For example, parser.add_argument('filename', help='Name of the file') requires the user to provide the filename as a positional argument.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › understanding command line arguments in python
Command Line Arguments in Python: Examples & Tips
🌐
Python
docs.python.org › 3 › library › argparse.html
argparse — Parser for command-line options, arguments and subcommands
Arguments read from a file must be one per line by default (but see also convert_arg_line_to_args()) and are treated as if they were in the same place as the original file referencing argument on the command line. So in the example above, the expression ['-f', 'foo', '@args.txt'] is considered equivalent to the expression ['-f', 'foo', '-f', 'bar'].
🌐
Tutorialspoint
tutorialspoint.com › python › python_command_line_arguments.htm
Python - Command-Line Arguments
C:\Python311>python hello.py Rajan argument list ['hello.py', 'Rajan'] Hello Rajan. How are you? The command-line arguments are always stored in string variables. To use them as numerics, you can them suitably with type conversion functions. In the following example, two numbers are entered as command-line arguments.
🌐
Real Python
realpython.com › python-command-line-arguments
Python Command-Line Arguments – Real Python
August 27, 2023 - Unless explicitly expressed at the command line with the option -o, a.out is the default name of the executable generated by the gcc compiler. It stands for assembler output and is reminiscent of the executables that were generated on older UNIX systems. Observe that the name of the executable ./main is the sole argument. Let’s spice up this example by passing a few Python command-line arguments to the same program:
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › command line arguments for your python script
Command Line Arguments for Your Python Script - MachineLearningMastery.com
June 21, 2022 - Additionally, we may also have compulsory arguments, which we just put into the command line. The part 192.168.0.3:/tmp/ and ./ above are examples. The order of compulsory arguments is important. For example, the rsync command above will copy files from 192.168.0.3:/tmp/ to ./ instead of the other way round. The following replicates the above example in Python using argparse:
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-command-line-arguments
Python Command Line Arguments: sys.argv, argparse, getopt | DigitalOcean
March 26, 2026 - This tutorial explains how to read and validate Python command line arguments using sys.argv, the argparse module (ArgumentParser), and the getopt module for Unix-style options.
Find elsewhere
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-main.html
Python main() - Command Line Arguments
Command line arguments like -affirm often select a mode or option for the run of the program, and these options typically begin with a dash as we have here. With the -hello option, the program prints a plain hello like this: ... With the -n option, the program prints the name a number of times, like this. $ python3 affirm.py -n 10 Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie $ python3 affirm.py -n 100 Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Maggie Ma
🌐
Stack Abuse
stackabuse.com › command-line-arguments-in-python
Command Line Arguments in Python
June 19, 2023 - For example, calling python arguments-getopt.py -vh is the same as calling python arguments-getopt.py -v -h. So in the last call above, the getopt module thought the user was trying to pass -e as an option, which is invalid. In this article we showed many different methods for retrieving command ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › understanding command line arguments in python
Command Line Arguments in Python: Examples & Tips
July 31, 2025 - Learn through python command line arguments example to manage script inputs efficiently, customize behavior, and boost your Python programming skills.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Medium
moez-62905.medium.com › the-ultimate-guide-to-command-line-arguments-in-python-scripts-61c49c90e0b3
The Ultimate Guide to Command Line Arguments in Python Scripts | by Moez Ali | Medium
April 18, 2023 - In Python, command-line arguments are accessed through the sys.argv list. The first item in the list is always the name of the script itself, and subsequent items are the arguments passed to the script. Here is an example script that prints out the command line arguments:
🌐
Codecademy
codecademy.com › docs › python › command line arguments
Python | Command Line Arguments | Codecademy
September 26, 2025 - Python provides several built-in modules to handle command line arguments, each with different levels of complexity and functionality. The sys.argv module provides the simplest way to access command line arguments. It stores all arguments passed to the script as a list of strings, where sys.argv[0] contains the script name itself. This example demonstrates how to access and use Python command line arguments using the sys module:
🌐
Codecademy
codecademy.com › article › command-line-arguments-in-python
Command Line Arguments in Python (sys.argv, argparse) | Codecademy
Learn how to use Python command line arguments with `sys.argv`, `getopt`, and `argparse`. Compare each method and build flexible, user-friendly scripts with real examples.
🌐
Analytics Vidhya
analyticsvidhya.com › home › 3 easy ways to handle command line arguments in python
3 Easy Ways to Handle Command Line Arguments in Python
January 15, 2025 - A. Command line arguments are values passed after the script name. Example: “python script.py arg1 arg2,” where arg1 and arg2 are command line arguments.
🌐
Medium
medium.com › @evaGachirwa › running-python-script-with-arguments-in-the-command-line-93dfa5f10eff
Running Python script with Arguments in the command line | by Eva Mwangi | Medium
April 22, 2023 - import argparse def get_sum_of_nums(num1,num2,num3): return(int(num1)+int(num2)+int(num3)) if __name__ == "__main__": parser = argparse.ArgumentParser( description="Script that adds 3 numbers from CMD" ) parser.add_argument("--num1", required=True, type=int) parser.add_argument("--num2", required=True, type=int) parser.add_argument("--num3", required=True, type=int) args = parser.parse_args() num1 = args.num1 num2 = args.num2 num3 = args.num3 print(get_sum_of_nums(num1, num2, num3)) Running the script from the command line.
🌐
nixCraft
cyberciti.biz › nixcraft › howto › linux › python command line arguments examples
Python Command Line Arguments Examples - nixCraft
March 7, 2016 - However, I do not recommend to use the for loop and phrase the argument method. It was included here for demonstration and historical purpose only. The correct and recommend Python syntax is as follows: #!/usr/bin/python __author__ = 'nixCraft' import sys, getopt # Store input and output file names ifile='' ofile='' # Read command line args myopts, args = getopt.getopt(sys.argv[1:],"i:o:") ############################### # o == option # a == argument passed to the o ############################### for o, a in myopts: if o == '-i': ifile=a elif o == '-o': ofile=a else: print("Usage: %s -i input -o output" % sys.argv[0]) # Display input and output file name passed as the args print ("Input file : %s and output file: %s" % (ifile,ofile) )
🌐
YouTube
youtube.com › dave gray
Python Command Line Arguments tutorial for Beginners - YouTube
Web Dev Roadmap for Beginners (Free!): https://bit.ly/DaveGrayWebDevRoadmapIn this Python command line arguments tutorial for beginners, you will learn how t...
Published   May 23, 2023
🌐
Old Dominion University
cs.odu.edu › ~tkennedy › cs330 › f25 › Public › pythonCmdLineArgs › index.html
Command Line Arguments in Python
Many programs will use command line arguments to specify input files. Consider the following excerpt… · if len(sys.argv) < 2: print("No input file provided.") print(f"Usage: {sys.argv[0]} input_file") sys.exit(1) with open(sys.argv[1], "r") as shapes_file: pass · The first conditional block checks that the required number of arguments were supplied by the user.
🌐
PythonForBeginners
pythonforbeginners.com › home › how to use sys.argv in python?
How to Use sys.argv in Python? - PythonForBeginners.com
June 11, 2023 - In the above example, you can observe that we have created a program sysargv.py and executed it using the command prompt. You can observe that the sys.argv list contains only one element i.e. the name of the program file. Now, let us change the program and add four command line arguments to the Python file while executing it.