Introduction
When working with files in Python, the built-in os module is great for basic tasks like creating dictionaries or checking if a file exists. However, when you need to perform high-level operations—like copying files, moving directories, or archiving data—the shutil (Shell Utilities) module is the standard tool for the job.
This guide explains the most common functions you will use in your daily coding.
Why Shutil?
The standard os module handles low-level operating system interactions. shutil builds upon this to providing user-friendly functions that mimic common shell commands (cp, mv, rm -rf).
Common Operations
1. Copying Files
To copy a file from one location to another, use shutil.copy().
import shutil
# Copy 'source.txt' to 'destination.txt'
# This preserves the file content and permissions.
shutil.copy("source.txt", "backup/source_backup.txt")
If you need to preserve all metadata (like creation time and modification time), use copy2 instead.
# 'copy2' is identical to 'copy' but also preserves file metadata
shutil.copy2("source.txt", "backup/source_exact_clone.txt")
2. Moving Files or Directories
The shutil.move() function is smart. It handles both files and directories, and it works across different filesystems.
# Move a file
shutil.move("old_folder/image.png", "new_folder/image.png")
# Move (rename) a folder
shutil.move("temp_data/", "processed_data/")
3. Deleting Entire Directories
This is one of the most powerful and dangerous functions. The standard os.rmdir() only works on empty directories. To delete a directory containing files, you use shutil.rmtree().
Warning: This operation is irreversible.
import shutil
import os
path_to_clean = "temp/cache_folder"
# Safety check: Always verify the path before deleting!
if os.path.exists(path_to_clean):
shutil.rmtree(path_to_clean)
print(f"Successfully deleted {path_to_clean}")
4. Copying Entire Directories
To copy a folder and all its contents recursively, use shutil.copytree().
# Copy the entire 'src' folder to a new 'src_backup' folder
shutil.copytree("src", "src_backup")
Note: The destination directory (src_backup in this example) must not exist before running this command; shutil will create it for you.
Summary
| Function | Purpose | Shell Equivalent |
|---|---|---|
shutil.copy(src, dst) | Copy a single file | cp src dst |
shutil.move(src, dst) | Move or rename a file/folder | mv src dst |
shutil.copytree(src, dst) | Recursively copy a directory | cp -R src dst |
shutil.rmtree(path) | Recursively delete a directory | rm -rf path |
Using shutil makes your file manipulation code cleaner, safer, and cross-platform compatible.