There are a few modules specialized in parsing command line arguments: getopt, optparse and argparse. optparse is deprecated, and getopt is less powerful than argparse, so I advise you to use the latter, it'll be more helpful in the long run.

Here's a short example:

import argparse

# Define the parser
parser = argparse.ArgumentParser(description='Short sample app')

# Declare an argument (`--algo`), saying that the 
# corresponding value should be stored in the `algo` 
# field, and using a default value if the argument 
# isn't given
parser.add_argument('--algo', action="store", dest='algo', default=0)

# Now, parse the command line arguments and store the 
# values in the `args` variable
args = parser.parse_args()

# Individual arguments can be accessed as attributes...
print args.algo

That should get you started. At worst, there's plenty of documentation available on line (say, this one for example)...

Answer from Pierre GM 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
Discussions

Modifying Python Script to Accept Command Line Arguments?
https://docs.python.org/3/library/argparse.html More on reddit.com
๐ŸŒ r/learnpython
5
2
March 3, 2024
Passing argument to function from command line
My son wrote a command line program in ruby for me. I want to convert it to python. To run the program he has done this on the command line; โ€œ./myProgram.rb โ€˜argumentโ€™โ€. Then the 'argument โ€™ is passed into the program to be processed. I want to do the same in python. More on discuss.python.org
๐ŸŒ discuss.python.org
8
0
June 25, 2025
Pass an argument to a Python script
Use sys.argv https://docs.python.org/3/library/sys.html#sys.argv More on reddit.com
๐ŸŒ r/learnpython
7
1
March 6, 2021
Argument passing in python script
Hey, Iโ€™m working with the invoke python method in UIPaths I want to pass 3 arg to python fuction 2 string and one python obj how can I do that?? More on forum.uipath.com
๐ŸŒ forum.uipath.com
0
0
September 22, 2023
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ modifying python script to accept command line arguments?
r/learnpython on Reddit: Modifying Python Script to Accept Command Line Arguments?
March 3, 2024 -

I am trying to modify an already existing code (https://github.com/ohyicong/decrypt-chrome-passwords/blob/main/decrypt_chrome_password.py) to take in command line arguments an example: python script.py "path_to_Local_State_file" "path_to_Login_Data_file" I think that this should be a pretty simple fix but chatGPT is not giving me the correct code when I test it how should I approach this.

๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Passing argument to function from command line - Python Help - Discussions on Python.org
June 25, 2025 - My son wrote a command line program in ruby for me. I want to convert it to python. To run the program he has done this on the command line; โ€œ./myProgram.rb โ€˜argumentโ€™โ€. Then the 'argument โ€™ is passed into the prograโ€ฆ
๐ŸŒ
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 - Command line arguments are a powerful tool for making your Python scripts more flexible and reusable. They allow you to pass args to python script different inputs to your script each time you run it, making it more versatile. Weโ€™ve explored three easy ways to handle command line arguments โ€“ using sys.argv, the getopt module, and the argparse module.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_command_line_arguments.htm
Python - Command-Line Arguments
C:\Python311>python parser2.py ... displays Hello Guest message. ... We can assign default value to an argument in add_argument() method....
Find elsewhere
๐ŸŒ
Python Course
python-course.eu โ€บ python-tutorial โ€บ passing-arguments.php
25. Passing Arguments | Python Tutorial | python-course.eu
November 8, 2023 - The passing of parameters and arguments in Python. Explaining the difference between call by object sharing, call by value and call by name.
๐ŸŒ
UiPath Community
forum.uipath.com โ€บ help โ€บ activities
Argument passing in python script - Activities - UiPath Community Forum
Hey, Iโ€™m working with the invoke python method in UIPaths I want to pass 3 arg to python fuction 2 string and one python obj how can I do that??
Published ย  September 22, 2023
๐ŸŒ
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 - The Argparse module provides more options as compared to the sys module. These options include: ... 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))
Top answer
1 of 3
24

You can use the sys module like this to pass command line arguments to your Python script.

import sys

name_of_script = sys.argv[0]
position = sys.argv[1]
sample = sys.argv[2]

and then your command line would be:

./myscript.py 10 100
2 of 3
16

Use argparse module:

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 and issues errors when users give the program invalid arguments.

It's pretty powerful: you can specify help messages, make validations, provide defaults..whatever you can imagine about working with command-line arguments.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-p", "--position", type=int)
parser.add_argument("-s", "--sample", type=int)

args = parser.parse_args()
col = args.position
sample = args.sample

print col
print sample

Here's what on the command-line:

$ python test.py --help
usage: test.py [-h] [-p POSITION] [-s SAMPLE]

optional arguments:
  -h, --help            show this help message and exit
  -p POSITION, --position POSITION
  -s SAMPLE, --sample SAMPLE

$ python test.py -p 10 -s 100
10
100
$ python test.py --position 10 --sample 100
10
100

Speaking about the code you've provided:

  • unused import random statement
  • move from random import shuffle to the top of the script
  • no need to call f.close() (especially with ;) - with handles closing the file automagically

Here's how the code would look like after the fixes:

#!/usr/bin/python
import argparse
import csv
from random import shuffle


parser = argparse.ArgumentParser()
parser.add_argument("-p", "--position", type=int)
parser.add_argument("-s", "--sample", type=int)

args = parser.parse_args()

with open('<filename>', 'r') as f:
    reader = csv.reader(f)
    data = [row[args.position] for row in reader]
    shuffle(data)
    print '\n'.join(data[:args.sample])
๐ŸŒ
OpenSourceOptions
opensourceoptions.com โ€บ how-to-pass-arguments-to-a-python-script-from-the-command-line
How to Pass Arguments to a Python Script from the Command Line โ€“ OpenSourceOptions
In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys (sys.argv) will store all the information in the command line entry and can be accessed inside the Python script.
๐ŸŒ
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
All parameters should be passed as keyword arguments. Each parameter has its own more detailed description below, but in short they are: prog - The name of the program (default: sys.argv[0]) usage - The string describing the program usage (default: generated from arguments added to parser)
๐ŸŒ
MachineLearningMastery
machinelearningmastery.com โ€บ home โ€บ blog โ€บ command line arguments for your python script
Command Line Arguments for Your Python Script - MachineLearningMastery.com
June 21, 2022 - In this tutorial, youโ€™ve seen how we can use the command line for more efficient control of our Python script. Specifically, you learned: How we can pass in parameters to your Python script using the argparse module
๐ŸŒ
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.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-execute-scripts-with-parameters-418940
How to execute scripts with parameters | LabEx
By understanding how to effectively ... Python scripting capabilities. Script arguments are input values passed to a Python script when it is executed from the command line.......
๐ŸŒ
Python GUI
pythongui.org โ€บ home โ€บ what are command line arguments and how to pass arguments to a python script
What Are Command Line Arguments And How To Pass Arguments To A Python Script
December 26, 2022 - Install PyScipter and appreciate the benefits it provides for creating and running Python scripts. To pass arguments to the Python script, you need to use the command line and specify the necessary arguments after the name of the script file.
๐ŸŒ
CodeGenes
codegenes.net โ€บ blog โ€บ how-to-pass-arguments-to-a-python-script
How to Pass Arguments to a Python Script โ€” codegenes.net
Use getopt if you are more familiar with the Unix-style option parsing or if you need to maintain compatibility with older Python code that uses getopt. It has a more complex syntax compared to argparse and lacks some of the advanced features like automatic help message generation. Validate arguments: Always validate the arguments passed to your script.