Command Line Arguments in Python

Week 6 - Python

Created: 2022-07-16
Tags: #permanent


Libraries needed below

from sys import argv

Explanation of how to use argv
argv means "argument vector", this is where we store the arguments.
argv[0] is the code execution itself (i.e ./filename)
len() counts in 1.
So two arguments here (filename and arg1)

if len(argv) == 2:
    print(f"hello, {argv[1]}")

Execution of the Script below

python file_name.py arg1 arg2 ...

References

  1. 112G