Shutil Module in Python

The shutil module in Python provides high-level file operations for copying, moving, and deleting files and directories. It offers a simple interface for common shell utilities, making file management tasks more efficient and cross-platform compatible.

Importing the Shutil Module

To use the shutil module, simply import it at the beginning of your program ?

import shutil
import os

# Check if shutil is available
print("Shutil module imported successfully")
print("Available functions:", [func for func in dir(shutil) if not func.startswith('_')][:5])
Shutil module imported successfully
Available functions: ['Error', 'ExecError', 'ReadError', 'RegistryError', 'SameFileError']

Copying Files

Using shutil.copy()

The shutil.copy() function copies a file from source to destination, preserving permissions but not metadata ?

import shutil
import os

# Create a sample file first
with open('sample.txt', 'w') as f:
    f.write('This is a sample file for demonstration.')

# Copy the file
shutil.copy('sample.txt', 'sample_copy.txt')

# Verify the copy
print("Original file exists:", os.path.exists('sample.txt'))
print("Copy file exists:", os.path.exists('sample_copy.txt'))

# Read content of copied file
with open('sample_copy.txt', 'r') as f:
    print("Copied file content:", f.read())
Original file exists: True
Copy file exists: True
Copied file content: This is a sample file for demonstration.

Using shutil.copy2()

The shutil.copy2() function is similar to copy() but also preserves metadata like timestamps ?

import shutil
import os
import time

# Create a file with content
with open('original.txt', 'w') as f:
    f.write('Original file with metadata')

# Copy using copy2 to preserve metadata
shutil.copy2('original.txt', 'original_copy2.txt')

# Check file stats
original_stat = os.stat('original.txt')
copy_stat = os.stat('original_copy2.txt')

print("Original modification time:", original_stat.st_mtime)
print("Copy modification time:", copy_stat.st_mtime)
print("Times match:", original_stat.st_mtime == copy_stat.st_mtime)
Original modification time: 1699123456.789
Copy modification time: 1699123456.789
Times match: True

Copying Directories

Use shutil.copytree() to recursively copy entire directory trees ?

import shutil
import os

# Create a directory structure
os.makedirs('source_dir/sub_dir', exist_ok=True)
with open('source_dir/file1.txt', 'w') as f:
    f.write('File 1 content')
with open('source_dir/sub_dir/file2.txt', 'w') as f:
    f.write('File 2 content')

# Copy the entire directory tree
shutil.copytree('source_dir', 'destination_dir')

# Verify the copy
print("Source directory exists:", os.path.exists('source_dir'))
print("Destination directory exists:", os.path.exists('destination_dir'))
print("Files in destination:", os.listdir('destination_dir'))
print("Files in sub_dir:", os.listdir('destination_dir/sub_dir'))
Source directory exists: True
Destination directory exists: True
Files in destination: ['file1.txt', 'sub_dir']
Files in sub_dir: ['file2.txt']

Moving Files and Directories

The shutil.move() function moves files or directories from one location to another ?

import shutil
import os

# Create a file to move
with open('move_me.txt', 'w') as f:
    f.write('This file will be moved')

print("Before move - file exists:", os.path.exists('move_me.txt'))

# Move the file (can also rename simultaneously)
shutil.move('move_me.txt', 'moved_file.txt')

print("After move - original file exists:", os.path.exists('move_me.txt'))
print("After move - moved file exists:", os.path.exists('moved_file.txt'))

# Create a directory and move it
os.makedirs('temp_dir', exist_ok=True)
shutil.move('temp_dir', 'relocated_dir')

print("Directory moved:", os.path.exists('relocated_dir'))
Before move - file exists: True
After move - original file exists: False
After move - moved file exists: True
Directory moved: True

Deleting Files and Directories

Use shutil.rmtree() to remove entire directory trees, including all contents ?

import shutil
import os

# Create a directory structure to delete
os.makedirs('delete_me/nested/deep', exist_ok=True)
with open('delete_me/file.txt', 'w') as f:
    f.write('Delete this file')

print("Before deletion:", os.path.exists('delete_me'))

# Remove the entire directory tree
shutil.rmtree('delete_me')

print("After deletion:", os.path.exists('delete_me'))

# For single files, use os.remove()
with open('single_file.txt', 'w') as f:
    f.write('Delete me too')

os.remove('single_file.txt')
print("Single file deleted:", not os.path.exists('single_file.txt'))
Before deletion: True
After deletion: False
Single file deleted: True

Working with File Permissions

Copy file statistics and permissions using shutil.copystat() ?

import shutil
import os

# Create source and destination files
with open('source_perms.txt', 'w') as f:
    f.write('Source file with permissions')

with open('dest_perms.txt', 'w') as f:
    f.write('Destination file')

# Change permissions on source file
os.chmod('source_perms.txt', 0o755)

# Copy only the statistics/permissions
shutil.copystat('source_perms.txt', 'dest_perms.txt')

# Check permissions
source_perms = oct(os.stat('source_perms.txt').st_mode)[-3:]
dest_perms = oct(os.stat('dest_perms.txt').st_mode)[-3:]

print("Source permissions:", source_perms)
print("Destination permissions:", dest_perms)
print("Permissions copied:", source_perms == dest_perms)
Source permissions: 755
Destination permissions: 755
Permissions copied: True

Comparison of Shutil Functions

Function Purpose Preserves Metadata
shutil.copy() Copy file with permissions No
shutil.copy2() Copy file with metadata Yes
shutil.copytree() Copy entire directory tree Yes
shutil.move() Move/rename files/directories Yes
shutil.rmtree() Remove directory tree N/A

Conclusion

The shutil module provides essential high-level file operations for Python applications. Use copy2() for files needing metadata preservation, copytree() for directories, and move() for relocating files. These functions offer cross-platform compatibility and simplified file management.

Updated on: 2026-03-27T10:06:41+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements