The OS Module

STEP 4: The os Module (Python’s Secret Backdoor Into Your Computer)

Alright young developer, you’ve been doing great.
You’ve mastered math, randomness, and even bent time like a budget Dr. Strange.
Feeling like a coding legend? Perfect.
Because now I’m handing you the os module — Python’s backstage pass to your computer.

This is the part where Python stops being a cute pet and becomes a system level ninja.
Use this power wisely. (Translation: don’t delete your entire Desktop. Your parents will not be impressed.)

What the os Module Actually Does

Think of os as Python whispering to your computer:

  • “Hey, what files are in this folder?”
  • “Where am I right now?”
  • “Can you make me a new folder?”
  • “Who’s logged in?”

It’s basically Python saying:
“I run this place now.”


1. Find Out Where Your Code Is Running

import os

print("Current working directory:", os.getcwd())
Try yourself.

This is Python’s version of checking Google Maps.Except it actually works.


2. List All Files in the Folder

import os

print("Files in this folder:", os.listdir())
Try yourself.

Boom — instant file list.

Great for:
  • loading game assets
  • checking save files
  • pretending you’re hacking the Pentagon (you’re not)

3. Create a New Folder (Yes, You Have That Power)

import os

os.mkdir("game_data")
print("Created a folder called game_data!")
Try yourself.

Congratulations — you just created a folder with code. You’re basically a wizard now.


4. Check If a File Exists (Before You Break Something)

import os

print("Does save.txt exist?", os.path.exists("save.txt"))
Try yourself.

This prevents your game from exploding when it tries to load a file that doesn’t exist.

Your future self will thank you.


Practice (Your Turn!)

Mini Challenge: Print Your Username

Add a line that prints:

Logged in as: <your username>

Use: os.getlogin()

If your username is something like “xXShadowMasterXx”…

I mean… live your truth.

Modify and Run