Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 12

OS Operations — Folders and Files with the os Module

Create, list, rename, and remove files and folders in Python with mkdir, makedirs, listdir, rename, remove, rmdir, and removedirs.

Intermediate25 min
OS module operations hero Module 16 Lesson 12 folders and files

The os module performs operating-system work: create a folder, list its names, rename a file, and remove what you no longer need.

Use the playground at the bottom of this page, then take the quiz.

What you will learn

  • mkdir and makedirs
  • listdir, rename, and remove
  • rmdir and removedirs
  • Which exceptions match a missing name, an existing name, or a folder that is not empty
  • Why a Windows backslash can be read as an escape

The idea

Build a folder, then put the path back

import os, then call the function that matches the job.

Function Job Typical failure
os.mkdir(name) Create one folder FileExistsError if it exists. FileNotFoundError if a parent folder is missing.
os.makedirs(path) Create a chain of folders FileExistsError if the final path exists
os.listdir(path) List names in a folder FileNotFoundError if the folder is missing
os.rename(old, new) Rename a file or folder FileNotFoundError if old is missing
os.remove(path) Delete a file FileNotFoundError if the file is missing
os.rmdir(name) Delete one empty folder OSError if the folder is not empty
os.removedirs(path) Delete an empty chain, from the leaf upward FileNotFoundError or OSError if a folder is missing or not empty

mkdir cannot create studio/drafts/notes in one call when studio does not exist yet. makedirs can.

rmdir refuses a folder that still contains files. Delete the files with remove first.

On Windows, a normal string such as "notes\new" treats \n as a newline, not as a folder separator. That raises OSError. Prefer forward slashes, which Python accepts on Windows, macOS, and Linux: "studio/drafts/notes". A raw string or doubled backslashes are the other fixes.

These examples stay inside a relative studio folder created by the program. They do not use a machine-specific drive path.

This is the last lesson of the module. You can now store text, bytes, pickled objects, CSV rows, and JSON dicts, and you can organize the folders around them.

Example

python
import os

leaf = "studio/drafts/notes"
os.makedirs(leaf)
note = leaf + "/memo.txt"
with open(note, "w", encoding="utf-8") as handle:
    handle.write("Draft note\n")

print(os.listdir(leaf))
renamed = leaf + "/memo-final.txt"
os.rename(note, renamed)
os.remove(renamed)
os.removedirs(leaf)
print("studio exists:", os.path.exists("studio"))

The playground clears any leftover studio folder from an earlier run, then builds the chain, renames the memo, and removes it. removedirs deletes notes, then drafts, then studio, because each one is empty. Press Run more than once.

Next: OOP Roadmap — classes, objects, constructors, and the rest of the path.

Key takeaways

  • mkdir creates one folder; makedirs creates a chain

  • listdir returns names; rename changes a name; remove deletes a file

  • rmdir deletes one empty folder; removedirs deletes an empty chain

  • A missing name raises FileNotFoundError; a taken name raises FileExistsError

  • Use forward slashes so \n is not read as a newline

  • Previous: JSON Files

Loading playground…

Check your understanding

OS module operations quiz

os.mkdir creates:

1 / 10