Python for DevOps
Using the os Module to Automate Filesystem Tasks

Problem 1 — Filter names starting with a specific letter
Given a list of names, print only the ones starting with "a". Same structure as yesterday's number filtering loop through, check a condition, print if true.
One new thing: name.startswith("a") - a string method that returns True or False. Slotted straight into the if condition.
names = ["alice", "bob", "charlie", "anna", "brian"]
for name in names:
if name.startswith("a"):
print(name)
Output: alice and anna. Clean.
Problem 2 — Print each name with its length
Same list. Print every name alongside how many characters it has.
No if this time every name gets printed, nothing is skipped. New method: len() returns the length of a string. New syntax: f-strings to mix text and variables in one print.
names = ["alice", "bob", "charlie", "anna", "brian"]
for name in names:
print(f"{name} has {len(name)} characters")
Now DevOps Python Began
The os module. This is where Python stops being about numbers and lists and starts touching the real world.
Three lines to start:
import os
print(os.getcwd()) # where am I right now
print(os.listdir()) # what files are here
print(os.path.exists("test")) # does this folder exist
Ran it. Got my current path, got a list of files in the folder, got False because test didn't exist yet.
Then one more line:
os.makedirs("test")
print(os.path.exists("test"))
Output: Folder created! and True.
Opened Windows Explorer. The folder was sitting there. Created by a Python script.
That is the moment it clicked. This is what DevOps automation actually is scripts that interact with systems so you do not have to do it manually.
If you are reskilling for the AI era too, follow along. Drop a comment on where you are in your journey.





