Copying files is a common task in Python when you need to back up data, duplicate files, or move content between directories.
In this article, you’ll learn multiple ways to copy files in Python, including how to copy single files, entire directories, and even file metadata like timestamps and permissions.
Method 1: Using shutil.copy() — Copy File Content and Permissions
import shutil
# Copy file content and permissions
source = 'report.txt'
destination = 'backup/report_copy.txt'
shutil.copy(source, destination)
print("File copied successfully!")
Output:
File copied successfully!
Why use this?
> Copies both file content and permission bits
> Simple one-line operation
> Creates the destination file automatically if it doesn’t exist
Method 2: Using shutil.copy2() — Copy with Metadata
If you also want to copy metadata (timestamps, creation, and modification times), use copy2() instead of copy().
import shutil
source = 'report.txt'
destination = 'backup/report_with_metadata.txt'
# Copies file + metadata (timestamps, permissions)
shutil.copy2(source, destination)
print("File copied with metadata successfully!")
Output:
File copied with metadata successfully!
Method 3: Copying Entire Directory Using shutil.copytree()
To copy all files and subfolders from one directory to another, use shutil.copytree().
import shutil
source_dir = 'project_data'
destination_dir = 'project_backup'
# Recursively copy folder and its contents
shutil.copytree(source_dir, destination_dir)
print("Folder copied successfully!")
Output:
Folder copied successfully!
Method 4: Copying Files Using pathlib
Python’s modern pathlib module can also handle file copying with shutil under the hood.
from pathlib import Path
import shutil
source = Path('data.csv')
destination = Path('backup/data_copy.csv')
# Copy file using Path objects
shutil.copy2(source, destination)
print("File copied using pathlib!")
Output:
File copied using pathlib!
Method 5: Copy Only File Content Using shutil.copyfile()
If you just need the file data (not permissions or metadata):
import shutil
source = 'config.ini'
destination = 'backup/config_copy.ini'
# Copies only content (no permissions or metadata)
shutil.copyfile(source, destination)
print("File copied (content only)!")
Output:
File copied (content only)!
Why use this?
> Fastest method for raw content copy
> Ideal for processing data files where metadata isn’t important.
Method 6: Copy with Progress Bar (for Large Files)
For big files, show progress using shutil.copyfileobj() with manual buffering.
import shutil
source = open('large_video.mp4', 'rb')
destination = open('backup/large_video_copy.mp4', 'wb')
# Copy with progress
shutil.copyfileobj(source, destination, length=1024*1024) # 1MB chunks
source.close()
destination.close()
print("Large file copied in chunks!")
Output:
Large file copied in chunks!
Why use this?
> Efficient for very large files
> Allows custom buffer sizes
> Can integrate with progress bars or logs.
Skip Existing Files or Handle Errors
Always handle exceptions to avoid overwriting or permission issues.
import shutil import os
source = 'report.txt'
destination = 'backup/report_copy.txt'
try:
if not os.path.exists(destination):
shutil.copy2(source, destination)
print("File copied successfully!")
else:
print("File already exists. Skipping copy.")
except PermissionError:
print("Permission denied while copying file.")
except FileNotFoundError:
print("Source file not found.")
Output:
File copied successfully!
Why use this?
> Prevents overwriting existing files
> Handles missing files gracefully
> Ideal for automation scripts and scheduled backups.
FAQs — Python: How to Copy Files
How to copy a file in Python using shutil?
The easiest way to copy a file in Python is using the shutil module:
import shutil
shutil.copy('source.txt', 'destination.txt')
This copies the contents and permissions of source.txt to destination.txt.
How to copy a file from one folder to another using Python?
Provide full source and destination paths when using shutil.copy():
import shutil
shutil.copy('C:/data/file.txt', 'C:/backup/file.txt')
This copies the file from one directory to another.
How to copy all files from one directory to another in Python?
Loop through the files and use shutil.copy() for each one:
import os, shutil
src = 'C:/source_folder'
dst = 'C:/destination_folder'
for file in os.listdir(src):
shutil.copy(os.path.join(src, file), dst)
This copies every file from the source to the destination folder.
How to copy an entire folder including subfolders in Python?
Use shutil.copytree() to copy the entire directory structure recursively:
import shutil
shutil.copytree('C:/source_folder', 'C:/backup_folder')
This duplicates all files and subdirectories to the new location.
How to overwrite an existing file while copying in Python?
shutil.copy() automatically overwrites existing files by default.
If you want to confirm before overwriting, add a check:
import os, shutil
if os.path.exists('destination.txt'):
print("File already exists!")
else:
shutil.copy('source.txt', 'destination.txt')
How to copy file metadata and permissions along with file content in Python?
Use shutil.copy2() instead of copy() to preserve metadata like timestamps and permissions:
import shutil
shutil.copy2('source.txt', 'destination.txt')
How to copy only certain file types (e.g. .txt files) using Python?
Filter files by extension while looping:
import os, shutil
for file in os.listdir('source'):
if file.endswith('.txt'):
shutil.copy(os.path.join('source', file), 'destination')
How to copy large files efficiently in Python?
For large files, shutil.copyfileobj() allows copying data in chunks to control memory usage:
import shutil
with open('bigfile.iso', 'rb') as src, open('copy.iso', 'wb') as dst:
shutil.copyfileobj(src, dst, length=1024*1024)
This copies the file in 1 MB chunks.
How to copy files with progress indication in Python?
Use tqdm for progress display:
from tqdm import tqdm
import shutil
with open('source.zip', 'rb') as src, open('dest.zip', 'wb') as dst:
for chunk in tqdm(iter(lambda: src.read(1024*1024), b'')):
dst.write(chunk)