102 — Writing Your First DevOps Script

Beginner

Write a Python script from scratch, add a shebang line so it runs directly from the terminal, and accept command-line arguments with sys.argv — the three foundational patterns every DevOps Python tool is built on.

Learning Objectives

1
Write a Python script file and run it with python3
2
Add a shebang line (#!/usr/bin/env python3) so the script runs directly from the terminal
3
Make a script executable with chmod +x
4
Read command-line arguments using sys.argv
5
Guard against missing arguments with a usage message and sys.exit(1)
Step 1

Set up the lesson 102 workspace

Create a directory for this lesson and activate the virtual environment from Lesson 101. Every lesson in this course reuses the same venv — confirm it is active before writing any code.

Commands to Run

mkdir -p ~/devops-python/lesson-102
cd ~/devops-python/lesson-102
source ~/devops-python/lesson-101/devops-env/bin/activate

What This Does

The ~/devops-python/ parent holds all lesson directories in this course.

Activating the Lesson 101 venv gives access to pip and any packages installed there, including requests installed in Lesson 101.

Expected Outcome

Your terminal prompt shows (devops-env).

Running pwd prints .../lesson-102 and which python3 points inside devops-env/bin/.

Pro Tips

  • 1
    If you created your venv in a different location in Lesson 101, adjust the source path accordingly.
  • 2
    Run `which python3` after activation to confirm the path points into devops-env — if it shows a system path, the venv is not active.
Was this step helpful?

All Steps (0 / 8 completed)