Command Line Arguments
Command line arguments let you pass information into a Python script when you run it from the terminal, without needing to hardcode values inside the script itself. This makes your programs more flexible and reusable.
Using sys.argv
The most common way to access command line arguments in Python is through the sys module.
import sys
print("Script name:", sys.argv[0])
print("Arguments:", sys.argv[1:])
If you run this script from the terminal as python script.py data.csv 2026, Python captures data.csv and 2026 as arguments you can use inside your program.
Why This Matters
- Run the same script on different input files without editing the code
- Automate data processing tasks as part of a larger pipeline
- Build simple command-line tools for everyday data science tasks
Coming Up
With the fundamentals of scripts and arguments in place, the next topic looks at how to accept input directly from a user while a program is running, along with the eval() function for evaluating expressions given as text.