Command line arguments let a script receive values when it starts.
Use argparse when a script needs named options, help text, defaults, or required values.
Arguments make automation scripts easier to reuse because users do not need to edit the source code for every run.
argparse builds a small command-line interface and converts text arguments into named values.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--name", required=True)
args = parser.parse_args()
print(f"Hello, {args.name}")
Run the file with --name Maya to pass a value from the terminal.
Arguments arrive as text, so use type= when the program needs an integer, float, or other conversion.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--limit", type=int, default=10)
args = parser.parse_args()
print(f"Reading {args.limit} rows")
If --limit is missing, the script uses 10.
Try this next
0 of 3 completed
sys.argv is useful to understand, but argparse is better for real scripts because it gives names, validation, defaults, and help text.
It reads the file path as an argument. Your code still opens and validates the file.
A flag is usually an option such as --verbose or --dry-run that changes behavior without needing a separate value.
Explore 500+ free tutorials across 20+ languages and frameworks.