Python:
Reading command line arguments
How to:
Using Python’s sys
module, you can snag those command line arguments easily. Here’s how to access them in your script:
import sys
# First argument is always the script name, so we skip it
arguments = sys.argv[1:]
# Do something with the arguments
print("You entered:", arguments)
Run your script like this:
python your_script.py these are your arguments
Sample Output:
You entered: ['these', 'are', 'your', 'arguments']
Deep Dive
Way back when, folks interacted with computers through command lines. That’s why most languages, Python included, have a way to read command line arguments. It’s how scripts were controlled before GUIs came along.
Python’s sys.argv
is handy, but for the fancier command-parsing dance, there’s argparse
. argparse
is a module for when you need more than the basics – like when your arguments need names, types, or default values.
Now, sys.argv
is just a list. Everything you pass is a string, no matter what. There’s no magic; if you want numbers, convert them yourself with something like int()
or float()
.
See Also
For more on sys.argv
and argparse
, check out the Python docs:
sys.argv
: https://docs.python.org/3/library/sys.html#sys.argvargparse
tutorial: https://docs.python.org/3/howto/argparse.html
And if you really want to dive head-first into command line interfaces:
- Click: https://click.palletsprojects.com/en/7.x/
- docopt: http://docopt.org/
Happy coding!