📁 Learn how to manipulate directories in Python using the os module. This includes getting and changing the current working directory, creating, renaming, and deleting directories, listing files, and traversing directory trees.
This section will help you interact with your file system in a cross-platform way — ideal for automation scripts, data processing, and file management tasks.
- 📍 How to get and change the current working directory
- 🔗 Use
os.path.join()andos.path.split()for cross-platform path handling - 📂 Check if a path is a valid directory
- ➕ Create new directories safely
- 🔄 Rename directories
- 🗑️ Delete empty directories
- 🔍 Traverse directory contents recursively using
os.walk() - 📋 List all files (or specific types) from a directory and its subdirectories
- 💡 Hidden tips and best practices for working with directories in Python
Use os.getcwd() to find out where your script is currently running.
🔹 Example:
import os
cwd = os.getcwd()
print(f"Current Working Directory: {cwd}")🔸 This is useful when working with relative paths or debugging file access issues.
Use os.chdir(path) to switch to a different directory.
🔹 Example:
os.chdir('/script')
print(os.getcwd()) # Output: /scriptFileNotFoundError.
Use os.path.join() to build paths that work across Windows, macOS, and Linux.
🔹 Join Example:
import os
path = os.path.join('data', 'files', 'input.txt')
print(path)
# Output: data/files/input.txt (Linux/macOS) or data\files\input.txt (Windows)🔹 Split Example:
parts = os.path.split(path)
print(parts) # ('data/files', 'input.txt') or ('data\\files', 'input.txt')💡 Always use these functions instead of hardcoding slashes (/ or \) to ensure compatibility.
Use os.path.exists() and os.path.isdir() to check if a path exists and is a directory.
🔹 Example:
dir_path = os.path.join("C:\\", "temp")
if os.path.exists(dir_path) and os.path.isdir(dir_path):
print(f"{dir_path} is a valid directory.")
else:
print(f"{dir_path} is not a valid directory.")🔸 These checks prevent errors when trying to access non-existent or invalid paths.
Use os.mkdir() to create a new directory.
🔹 Best Practice – Only create if it doesn't exist:
dir = os.path.join("C:\\", "temp", "python")
if not os.path.exists(dir):
os.mkdir(dir)
print(f"Directory '{dir}' created.")🔸 To create nested directories at once, use os.makedirs():
os.makedirs(os.path.join("project", "data", "raw"), exist_ok=True)Use os.rename(old_path, new_path) to rename or move a directory.
🔹 Example:
old_dir = os.path.join("C:\\", "temp", "python")
new_dir = os.path.join("C:\\", "temp", "python3")
if os.path.exists(old_dir) and not os.path.exists(new_dir):
os.rename(old_dir, new_dir)
print(f"'{old_dir}' was renamed to '{new_dir}'")FileExistsError.
Use os.rmdir(path) to delete an empty directory.
🔹 Example:
dir = os.path.join("C:\\", "temp", "python")
if os.path.exists(dir):
os.rmdir(dir)
print(f"{dir} has been removed.")🔸 To delete a directory and all its contents, use shutil.rmtree():
import shutil
shutil.rmtree(dir) # Removes directory even if not emptyUse os.walk() to traverse directories and list files/subdirectories.
🔹 Example – Print all files and folders:
for root, dirs, files in os.walk("c:\\temp"):
print(f"{root} has {len(files)} files")🔸 This function is essential for recursive file operations like searching, copying, or batch processing.
for root, _, files in os.walk("D:\\web"):
for file in files:
print(file)html_files = []
for root, _, files in os.walk("D:\\web"):
for file in files:
if file.endswith(".html"):
html_files.append(os.path.join(root, file))
for f in html_files:
print(f)def list_files(path, extensions=None):
"""List files in path. If extensions provided, filter by them."""
for root, _, files in os.walk(path):
for file in files:
if extensions is None:
yield os.path.join(root, file)
else:
for ext in extensions:
if file.endswith(ext):
yield os.path.join(root, file)
# Usage
for f in list_files(r"D:\web", (".html", ".css")):
print(f)🔸 Using a generator (yield) makes this function memory-efficient for large directories.
import shutil
backup_dir = "D:\\backup"
os.makedirs(backup_dir, exist_ok=True)
for src_file in list_files("D:\\documents", [".txt"]):
dst_file = os.path.join(backup_dir, os.path.basename(src_file))
shutil.copy2(src_file, dst_file)
print(f"Copied {src_file} to {dst_file}")💡 Hidden Tips & Notes
- 🧩 Use
exist_ok=Truewithos.makedirs()to avoid exceptions if the directory already exists. - 🧵 Prefer generators over lists for large file sets.
- 📁 Avoid hardcoded paths — always use
os.pathfunctions for portability. - 🚫
os.rmdir()only works on empty directories. - 📦 Use
shutilfor advanced operations like copy, move, or remove entire directory trees. - 🧪 Wrap file/directory operations in
try...exceptblocks to handle permission errors gracefully.
🎉 Congratulations! You now have a solid understanding of how to manipulate directories and files in Python, including how to list, create, rename, and delete directories, and how to search and process files recursively.
Next up: 💬 Section 14: Strings – learn about raw strings, escape characters, and how to work with special characters.
