Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Created January 29, 2019 16:55
Show Gist options
  • Save bradmontgomery/1d05b7621cad42d923d5cf6a32149db7 to your computer and use it in GitHub Desktop.
Save bradmontgomery/1d05b7621cad42d923d5cf6a32149db7 to your computer and use it in GitHub Desktop.
Illustrating sys.argv in python
"""
This script illustrates how to use sys.argv to
capture command-line arguments.
Dowload & run this file like so:
python example.py
OR like this:
python example.py hello
OR like this:
python example.py one two 3 4 yippee!
"""
import sys # We must import sys in order to use it.
if __name__ == "__main__":
# All of the arguments provided to this script will be
# contained in a variable called: sys.argv
print(f"sys.argv = {sys.argv}")
# it's a list, so we can find it's lenght:
num_items = len(sys.argv)
print(f"There are {num_items} items in sys.argv")
# Or, we can access individual items (assuming they exist)
if num_items >= 2:
item = sys.argv[1]
print(f"The first argument to this script is: {item}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment