Preserving Directory Structure While Copying Files in Python – version 2
When copying files from one directory to another in Python, it’s important to maintain the original directory structure, especially when dealing with nested directories. In this post, we’ll explore how to use Python’s shutil and os libraries to copy files while preserving the directory structure.
Problem
Imagine you have a source directory with nested folders and files, and you want to copy all the files into a destination folder while maintaining the same folder structure. If you don’t preserve the directory structure, all files will be copied into a single directory, which might lead to confusion or data organization issues.
Solution
We can achieve this by using the shutil.copy2() function, which allows us to copy files while preserving metadata like timestamps. To ensure the directory structure is maintained, we use os.makedirs() to create any missing directories in the destination path before copying each file.
Here’s a simple Python script to accomplish this:
import os
import shutil
def copy_with_structure(src_dir, dest_dir):
# Walk through the source directory
for root, dirs, files in os.walk(src_dir):
# Create the corresponding directory structure in the destination directory
relative_path = os.path.relpath(root, src_dir)
dest_path = os.path.join(dest_dir, relative_path)
os.makedirs(dest_path, exist_ok=True)
# Copy each file to the destination, maintaining structure
for file in files:
src_file_path = os.path.join(root, file)
dest_file_path = os.path.join(dest_path, file)
shutil.copy2(src_file_path, dest_file_path)
print(f"Copied {src_file_path} to {dest_file_path}")
# Example usage
source_directory = "/path/to/source_directory"
destination_directory = "/path/to/destination_directory"
copy_with_structure(source_directory, destination_directory)
How It Works
- os.walk(): This function recursively traverses the source directory, yielding a tuple of the current directory (
root), subdirectories (dirs), and files (files). This allows us to go through each directory and its files. - os.makedirs(): Before copying files, we ensure that the corresponding directory structure exists in the destination directory. The
os.makedirs()function creates the directory path, andexist_ok=Trueensures that no error is raised if the directory already exists. - shutil.copy2(): This function copies files from the source to the destination while preserving metadata like the original file’s modification and access times.
Conclusion
By combining os.walk(), os.makedirs(), and shutil.copy2(), we can efficiently copy files while preserving the directory structure. This solution is useful in scenarios where maintaining the organization of files is critical.