In this article, you’ll learn multiple ways to copy a file to another directory using Python’s built-in modules — shutil, os, and pathlib.
Method 1: Using shutil.copy() (Most Common Way)
import shutil import os
source = 'reports/summary.txt'
destination_dir = 'backup/reports/'
# Make sure the destination directory exists
os.makedirs(destination_dir, exist_ok=True)
# Copy file to destination directory
shutil.copy(source, destination_dir)
print("File copied successfully!")
Output:
File copied successfully!
Why use this?
> Copies both file content and permissions
> Automatically saves file inside the target directory
> Creates the folder if it doesn’t exist
Method 2: Using shutil.copy2() (Copy + Metadata)
copy2() works just like copy() but preserves metadata like timestamps, permissions, and file creation info.
import shutil import os
source = 'reports/summary.txt'
destination_dir = 'backup/reports/'
os.makedirs(destination_dir, exist_ok=True)
# Copies file + metadata
shutil.copy2(source, destination_dir)
print("File copied with metadata successfully!")
Output:
File copied with metadata successfully!
Method 3: Copy and Rename in Destination
You can rename the file while copying to another directory.
import shutil import os
source = 'reports/summary.txt'
destination_dir = 'backup/reports/summary_backup.txt'
# Copy and rename the file
shutil.copy2(source, destination_dir)
print("File copied and renamed successfully!")
Output:
File copied and renamed successfully!
This method is useful when you want to version or rename files while copying.
Method 4: Using pathlib for Modern Code
The pathlib module provides a cleaner, object-oriented way to handle paths and directories.
from pathlib import Path import shutil
source = Path('reports/summary.txt')
destination_dir = Path('backup/reports')
# Ensure target directory exists
destination_dir.mkdir(parents=True, exist_ok=True)
# Copy file using Path objects
shutil.copy2(source, destination_dir / source.name)
print("File copied using pathlib!")
Output:
File copied using pathlib!
Why use this?
> More readable and Pythonic
> Works across Windows, macOS, and Linux
> Handles path joins safely without manual string concatenation
Method 5: Copy Multiple Files to Another Directory
If you want to copy several files at once:
import shutil import os
files = ['data/report1.csv', 'data/report2.csv', 'data/report3.csv']
destination_dir = 'backup/data/'
os.makedirs(destination_dir, exist_ok=True)
for file in files:
shutil.copy(file, destination_dir)
print("All files copied successfully!")
Output:
All files copied successfully!
Method 6: Handle Errors and Check Before Copying
Always handle missing files and permission errors gracefully.
import shutil import os
source = 'reports/summary.txt'
destination_dir = 'backup/reports/'
try:
os.makedirs(destination_dir, exist_ok=True)
if os.path.exists(source):
shutil.copy2(source, destination_dir)
print("File copied successfully!")
else:
print("Source file not found.")
except PermissionError:
print("Permission denied while copying the file.")
Output:
File copied successfully!
FAQs — Python: Copy File to Another Directory
How to copy a file to another directory in Python using shutil?
The simplest way to copy a file to another folder in Python is by using the shutil module:
import shutil
shutil.copy('C:/source/file.txt', 'C:/destination/file.txt')
This copies the file from the source path to the destination path.
How to copy a file to another directory while keeping the same filename in Python?
Just specify the destination folder instead of a new filename:
import shutil
shutil.copy('C:/source/file.txt', 'C:/backup/')
This keeps the same filename and places it in the target directory.
How to copy a file from one folder to another using an absolute path in Python?
Provide complete file paths for both source and destination:
import shutil
src = r'C:\users\data\report.pdf'
dst = r'C:\backup\report.pdf'
shutil.copy(src, dst)
How to copy multiple files to another directory in Python?
Loop through filenames and use shutil.copy() for each file:
import os, shutil
src = 'C:/source_folder'
dst = 'C:/target_folder'
for file in os.listdir(src):
shutil.copy(os.path.join(src, file), dst)
How to copy files with metadata (timestamps and permissions) to another directory?
Use shutil.copy2() to preserve metadata while copying:
import shutil
shutil.copy2('C:/source/file.txt', 'C:/destination/file.txt')
This keeps original timestamps and file permissions intact.
How to copy a file to another directory only if it doesn’t exist there?
Check for the file’s existence before copying:
import os, shutil
src = 'file.txt'
dst = 'C:/backup/file.txt'
if not os.path.exists(dst):
shutil.copy(src, dst)
How to copy files to another directory and rename them in Python?
Provide a new name in the destination path:
import shutil
shutil.copy('data.csv', 'C:/backup/data_backup.csv')
This copies and renames the file at the same time.
How to copy files from one directory to another using Pathlib in Python?
Use pathlib for a modern approach:
from pathlib import Path
import shutil
src = Path('file.txt')
dst = Path('backup') / src.name
shutil.copy(src, dst)
How to copy large files efficiently between directories in Python?
For large files, copy in chunks using shutil.copyfileobj():
import shutil
with open('source.iso', 'rb') as src, open('backup/source.iso', 'wb') as dst:
shutil.copyfileobj(src, dst, length=1024*1024)
This copies the file in 1MB chunks, reducing memory usage.