Working with os and shutil
Automate file and directory operations.
Working with os and shutil is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Working with os and shutil
Python’s os and shutil modules provide powerful tools for interacting with the file system, managing directories, and automating file-related tasks.
In this lesson, you’ll learn how to use these modules for tasks like file manipulation, directory management, and more.

2
What Is the os Module?
The os module provides functions for interacting with the operating system, such as creating directories, deleting files, and getting environment variables.
3
Creating Directories
You can create a new directory using the os.mkdir() or os.makedirs() functions:
# Creating directories
import os
os.mkdir("new_folder") # Creates a single directory
os.makedirs("nested/folder/structure") # Creates nested directories4
Listing Directory Contents
Use the os.listdir() function to list the contents of a directory:
# Listing directory contents
files = os.listdir(".") # Lists files and directories in the current directory
print(files)5
Working with shutil
The shutil module is designed for high-level file operations, such as copying, moving, and deleting files or directories.
6
Copying Files
Use the shutil.copy() function to copy files:
# Copying files
import shutil
shutil.copy("source.txt", "destination.txt") # Copies source.txt to destination.txt7
Moving and Renaming Files
Use the shutil.move() function to move or rename files:
# Moving and renaming files
shutil.move("source.txt", "archive/source_renamed.txt")8
Deleting Files and Directories
Use the os.remove() and shutil.rmtree() functions to delete files and directories:
# Deleting files and directories
os.remove("file_to_delete.txt") # Deletes a single file
shutil.rmtree("folder_to_delete") # Deletes a folder and its contents9
10
Common Mistakes with os and shutil
Here are some mistakes to avoid:
- Not checking if a file or directory exists before trying to delete or copy it.
- Overwriting files unintentionally while copying or moving.
- Using hardcoded paths instead of dynamic paths with
os.path.
11
What Did We Learn?
In this lesson, you learned:
- How to use the
osmodule for directory and file management. - How to use the
shutilmodule for high-level file operations like copying and moving. - Best practices to avoid common mistakes with file system automation.
Great job! Let’s move to the next topic.

Frequently asked questions
Is the “Working with os and shutil” lesson free?
Yes — the full text of “Working with os and shutil” is free to read here on the web, and the Learn AI with Python course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Working with os and shutil”?
Automate file and directory operations. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Working with os and shutil” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Automating Tasks with Scripts
- Working with os and shutil
- Automating Emails and Reports