Skip to main content

Command Palette

Search for a command to run...

Day 98: Python OS Module

Using the Python os Module for DevOps Tasks

Published
2 min read
Day 98: Python OS Module

Today you’ll learn about the os module in Python — a built-in module that lets you interact with the operating system, making it very useful in automation and DevOps scripting.


🧠 What is the os Module?

The os module provides a way to:

  • Work with environment variables

  • Handle file and directory paths

  • Execute system commands

  • Manage processes

This is especially useful in DevOps tasks like:

  • Creating automation scripts for deployment

  • Reading system info (like environment variables)

  • Creating/deleting directories for builds

  • Executing shell commands in CI/CD pipelines


⚙️ Commonly Used Functions

📂 Working with Directories

import os

# Get current working directory
print(os.getcwd())

# List files in current directory
print(os.listdir())

# Create a new directory
os.mkdir("build_dir")

# Remove a directory
os.rmdir("build_dir")

# Change directory
os.chdir("..")

⚡ Running System Commands

import os

# Run a system command
exit_code = os.system("echo Hello DevOps!")
print("Command exit code:", exit_code)

🌍 Environment Variables

import os

# Access environment variables
print(os.environ)

# Get a specific variable
print(os.environ.get("HOME"))

# Set a new variable
os.environ["STAGE"] = "production"

💡 Use Cases in DevOps

  • Creating directory structures for builds or deployments

  • Reading credentials from environment variables

  • Executing shell commands during build scripts

  • Cleaning up logs or temp directories automatically

DevOps overview as a beginner

Part 1 of 50

Sharing my journey of learning DevOps as a beginner — covering essential tools, cloud setup, CI/CD, Docker, monitoring, and more, step by step with practical examples.