The shutil module provides high-level functions for managing files and directories. It allows to perform operations such as copying, moving, renaming and deleting files or entire directory structures with simple Python commands.
Importing Module
Before using any function from the shutil module, you need to import it into your Python program. Since shutil is part of Python's standard library, no additional installation is required.
import shutil
You can also import specific functions if you only need a few of them:
from shutil import copy, move
Common Shutil Functions
shutil module provides several high-level functions for working with files and directories. These functions help perform tasks such as copying, moving, deleting and archiving files with minimal code.
1. shutil.copy(): copies a file from one location to another. It copies the file content and permissions but does not preserve metadata such as creation or modification times.
import shutil
shutil.copy("report.txt", "backup/report.txt")
print("File copied successfully")
Output
File copied successfully
Explanation: shutil.copy() copies report.txt to the backup directory. If the destination file does not exist, it will be created.
2. shutil.copy2(): works like shutil.copy() but also preserves file metadata such as modification time and access time.
import shutil
shutil.copy2("report.txt", "backup/report.txt")
print("File copied with metadata")
Output
File copied with metadata
Explanation: shutil.copy2() copies both the file contents and its metadata to the destination.
3. shutil.copytree(): copies an entire directory along with all its files and subdirectories.
import shutil
shutil.copytree("projects", "projects_backup")
print("Directory copied")
Output
Directory copied
Explanation: shutil.copytree() creates a complete copy of the projects directory and all its contents in projects_backup.
4. shutil.move(): moves a file or directory from one location to another. It can also be used to rename files and folders.
import shutil
shutil.move("report.txt", "documents/report.txt")
print("File moved")
Output
File moved
Explanation: shutil.move() transfers the file to a new location. If the destination is in the same directory with a different name, it effectively renames the file.
5. shutil.rmtree(): removes an entire directory along with all its files and subdirectories.
import shutil
shutil.rmtree("old_backup")
print("Directory deleted")
Output
Directory deleted
Explanation: shutil.rmtree() permanently deletes the specified directory and everything inside it.
6. shutil.make_archive(): creates an archive file such as ZIP or TAR from a directory.
import shutil
shutil.make_archive("backup", "zip", "projects")
print("Archive created")
Output
Archive created
Explanation: This creates a ZIP archive named backup.zip containing all files and folders from the projects directory.
7. shutil.unpack_archive(): extracts files from an archive.
import shutil
shutil.unpack_archive("backup.zip", "extracted_files")
print("Archive extracted")
Output
Archive extracted
Explanation: shutil.unpack_archive() extracts the contents of backup.zip into the extracted_files directory.
Additional Useful Shutil Functions
Besides the commonly used functions discussed above, the shutil module provides several other utilities for advanced file and directory management.
| Function | Description |
|---|---|
| shutil.copyfile() | Copies only the contents of a file |
| shutil.copymode() | Copies permission bits from one file to another |
| shutil.copystat() | Copies file metadata such as timestamps and permissions |
| shutil.disk_usage() | Returns disk usage statistics for a given path |
| shutil.chown() | Changes the owner and group of a file or directory |
| shutil.which() | Finds the path of an executable command |
| shutil.get_archive_formats() | Returns supported archive formats |
| shutil.get_unpack_formats() | Returns supported archive extraction formats |
Error Handling in Shutil Operations
File and directory operations may fail if files are missing, permissions are restricted or destination paths already exist. Using exception handling helps prevent program crashes.
import shutil
try:
shutil.copy("report.txt", "backup/report.txt")
print("File copied successfully")
except FileNotFoundError:
print("Source file not found")
except PermissionError:
print("Permission denied")
Output (if file is missing)
Source file not found
Explanation: The try-except block catches common file-related errors and displays a meaningful message instead of terminating the program.