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
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ argparse.html
argparse โ€” Parser for command-line options, arguments and subcommands
The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages.
Discussions

Command line arguments
I am a beginner at Python and am wondering what is the absolute fastest way to get command line arguments into the script. I am trying to build it for speed. commandline$ python3.7 script.py arg1 arg2 arg3 All I need is those args assigned to a variable so I can use them later on in the script. More on discuss.python.org
๐ŸŒ discuss.python.org
0
0
January 10, 2022
Python 3: test command line arguments - Stack Overflow
I'm newbie to Python. I'd like to code a script running on Linux. To test if user enter all the script arguments: If user type: myscript => print "Usage: myscript [Dir] [Old] [New]" If user type: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Calling command line arguments in Python 3 - Stack Overflow
I am writing a program in Python 3 that will take data from a csv file, make adjustments to it and write the new data to a csv file that will be named by the user. I'm just trying to understand the More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 17, 2017
Python beginner: what are command lines arguments and what purpose do they serve in this code
Command line arguments are what you pass to a script when calling it from a windows/linux/whatever command line. For example, if this script was in a file called pw.py, i would call it from a windows command line with this: python pw.py luggage In python, the first command line argument is always the name of the script ('pw.py' in this case). The next argument is 'luggage' More on reddit.com
๐ŸŒ r/learnpython
4
4
January 7, 2016
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_command_line_arguments.htm
Python - Command-Line Arguments
Python Command Line Arguments provides a convenient way to accept some information at the command line while running the program. We usually pass these values along with the name of the Python script.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-main.html
Python main() - Command Line Arguments
$ python3 affirm.py -affirm Lisa Everything is coming up Lisa $ python3 affirm.py -affirm Bart Looking good Bart $ python3 affirm.py -affirm Maggie Today is the day for Maggie $ Command line arguments, or "args", are extra information typed on the line when a program is run.
๐ŸŒ
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
๐ŸŒ
Real Python
realpython.com โ€บ python-command-line-arguments
Python Command-Line Arguments โ€“ Real Python
August 27, 2023 - In this first example, the Python interpreter takes option -c for command, which says to execute the Python command-line arguments following the option -c as a Python program. Another example shows how to invoke Python with -h to display the help: ... $ python -h usage: python3 [option] ...
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ command-line-arguments-in-python
Command Line Arguments in Python (sys.argv, argparse) | Codecademy
These arguments are always treated as strings, so they may need to be converted to the appropriate data types when working with numbers or other values. ... import sys # Accessing command-line arguments arguments = sys.argv # List of arguments script_name = sys.argv[0] # Name of the script # Checking if an argument is provided before accessing it if len(sys.argv) > 1: first_argument = sys.argv[1] # First argument else: first_argument = None # No argument provided
Find elsewhere
๐ŸŒ
Python
docs.python.org โ€บ 3.3 โ€บ library โ€บ argparse.html
16.4. argparse โ€” Parser for command-line options, arguments and sub-commands โ€” Python 3.3.7 documentation
Filling an ArgumentParser with information about program arguments is done by making calls to the add_argument() method. Generally, these calls tell the ArgumentParser how to take the strings on the command line and turn them into objects. This information is stored and used when parse_args() ...
๐ŸŒ
GitBooks
learnbyexample.gitbooks.io โ€บ python-basics โ€บ content โ€บ Command_line_arguments.html
Command line arguments ยท Python Basics
$ ./line_count.py if_elif_else.py No. of lines in 'if_elif_else.py' is: 22 $ ln -s line_count.py word_count.py $ ./word_count.py if_elif_else.py No. of words in 'if_elif_else.py' is: 73 $ ln -s line_count.py abc.py $ ./abc.py if_elif_else.py Program name './abc.py' not recognized $ wc -lw if_elif_else.py 22 73 if_elif_else.py ยท #!/usr/bin/python3 import argparse, subprocess parser = argparse.ArgumentParser() parser.add_argument('-f', '--file', help="file to be sorted", required=True) parser.add_argument('-u', help="sort uniquely", action="store_true") args = parser.parse_args() if args.u: subprocess.call(['sort', '-u', args.file, '-o', args.file]) else: subprocess.call(['sort', args.file, '-o', args.file])
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Command line arguments - Python Help - Discussions on Python.org
January 10, 2022 - I am a beginner at Python and am wondering what is the absolute fastest way to get command line arguments into the script. I am trying to build it for speed. commandline$ python3.7 script.py arg1 arg2 arg3 All I need is those args assigned to a variable so I can use them later on in the script.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ using โ€บ cmdline.html
1. Command line and environment โ€” Python 3.14.4 documentation
An interface option terminates the list of options consumed by the interpreter, all consecutive arguments will end up in sys.argv โ€“ note that the first element, subscript zero (sys.argv[0]), is a string reflecting the programโ€™s source. ... Execute the Python code in command.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ command line arguments
Python | Command Line Arguments | Codecademy
September 26, 2025 - 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:
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-command-line-arguments
Python Command Line Arguments: sys.argv, argparse, getopt | DigitalOcean
3 weeks ago - Learn Python command line arguments with sys.argv, argparse, and getopt. Compare patterns, handle bad input, and copy the examples.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python beginner: what are command lines arguments and what purpose do they serve in this code
r/learnpython on Reddit: Python beginner: what are command lines arguments and what purpose do they serve in this code
January 7, 2016 -

Hey guys

I've been reading 'How to automate the boring stuff with Python' and reached the 6th chapter. I've understood everything well so far but in the 6th chapter there's an exercise where we have to create a 'password locker', and the code goes like this:

#! python3
# pw.py - An insecure password locker program.
 PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
            'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
           'luggage': '12345'}
    
    import sys, pyperclip
    if len(sys.argv) < 2:
        print('Usage: py pw.py [account] - copy account password')
        sys.exit()
    
    account = sys.argv[1]   # first command line arg is the account name
    
    if account in PASSWORDS:
        pyperclip.copy(PASSWORDS[account])
        print('Password for ' + account + ' copied to clipboard.')
    else:
        print('There is no account named ' + account)

And the text to go along with it:

The command line arguments will be stored in the variable sys.argv. (See Appendix B for more information on how to use command line arguments in your programs.) The first item in the sys.argv list should always be a string containing the programโ€™s filename ('pw.py'), and the second item should be the first command line argument. For this program, this argument is the name of the account whose password you want. Since the command line argument is mandatory, you display a usage message to the user if they forget to add it (that is, if the sys.argv list has fewer than two values in it). Make your program look like the following:

I've been trying to understand what he means but I'm so confused. What are command line arguments? Why is there a check to see if there are less than two? What purpose do the shebang lines serve in the beginning?

Thanks a lot in advance!!

๐ŸŒ
Computer Science Atlas
csatlas.com โ€บ python-argparse-positional-command-line-arguments
Python 3: Accept Positional Command Line Arguments โ€” Computer Science Atlas
June 16, 2021 - The argparse module allows us to accept command line arguments. Here's how we can write hello.py using argparse: import argparse parser = argparse.ArgumentParser() parser.add_argument('name') args = parser.parse_args() name = args.name print( f'Hello, { name }!' ) If the user runs hello.py ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ command-line-arguments-in-python
Command Line Arguments in Python
June 19, 2023 - Next, from the list of input parameters we remove the first list element (see the code below), and store the remaining list of command line arguments in the variable called argument_list: # Include standard modules import getopt, sys # Get full command-line arguments full_cmd_arguments = sys.argv # Keep all but the first argument_list = full_cmd_arguments[1:] print argument_list
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ accessing-command-line-arguments-python
Accessing command-line arguments in Python - Python Morsels
August 30, 2021 - When you run a program from your system command-line, you can pass in arguments to the program. Here we have a program called greet.py: print("Hello world") Here we're calling this program: $ python3 greet.py Hello world ยท The word "argument" is a loaded term.