Q1
What is `sys.argv`? How do you pass and parse command-line arguments in scripts?
💬Answer
sys.argv is a list containing the command-line arguments passed to the active Python script.
sys.argv[0]: The name of the script itself.sys.argv[1]: The first argument passed after the script name.
# Save as test.py
import sys
print(f"Script Name: {sys.argv[0]}")
if len(sys.argv) > 1:
print(f"First Arg: {sys.argv[1]}")
Running python3 test.py prod will output:
Script Name: test.py
First Arg: prod
Related Python Questions
View All PythonQuestions →
Created by
Apurv Gujjar
DevOps & Cloud Engineer
Specialized in:DevOpsAWSGCPKubernetesTerraformDocker
View Portfolio