Examples
- How to Create Directory in Python? New and If Not Exists
- How to Change Directory in Python? Change Current Working Directory
- How to Get Current Working Directory in Python?
- How to Remove Directory in Python? (Empty & With Files)
- How to Copy Directory in Python? Program With Examples
- How to Open Directory in Python? Program & Example
- How to Read All Files in Directory in Python? (All Types of Files)
- How to Copy File in Python From One Directory to Another? (With and Without shutil)
- Python File Append Program (Append to File)
- How to Read Text File in Python Line by Line? (3 Ways)
- How to Get File Name From File Path in Python?
- Get Number of Lines in File in Python (Line Count)
- How to Get Size of File in Python? (File Size Program)
How to Copy Directory in Python? Program With Examples
Copying directories is a common task when working with file systems in Python. It allows you to create a duplicate of a directory, including all its files and subdirectories, in a different location. In this post, we will explore different approaches to copy directory in Python.
Python provides several methods for copying directories, including the shutil.copytree() function and the distutils.dir_util.copy_tree() function. These functions offer convenient ways to copy directories, whether they are small or contain complex directory structures.
Let’s learn the process of copying directories step by step. We will explain how to use both the shutil.copytree() function and the distutils.dir_util.copy_tree() function.
Program to Copy Directory in Python (Using shutil)
The shutil module provides functions for file operations. Specifically, the shutil.copytree() function can be used to recursively copy a directory and its contents to a new location.
Code
import shutil
# Specify the source and destination directories
source_directory = '/path/to/source_directory'
destination_directory = '/path/to/destination_directory'
# Copy the directory and its contents
shutil.copytree(source_directory, destination_directory)
print("Directory copied successfully!")
Output
Directory copied successfully!
Explanation
In this program, the shutil.copytree() function is used to copy the directory and its contents from the source_directory to the destination_directory. The function performs a recursive copy, meaning it copies all files and subdirectories within the source directory.
After executing shutil.copytree(), the specified directory and its contents will be duplicated in the destination location. Make sure that the destination directory does not already exist, as it will raise an error in that case.
The program concludes by printing "Directory copied successfully!" to indicate that the copy operation was successful.
Note: Please note that the shutil.copytree() function will preserve the file permissions, timestamps, and other metadata of the original files during the copy process.
Ensure that you have the necessary permissions and sufficient disk space in the destination location before performing the directory copy operation.
Copy Directory in Python (Using distutils module)
Here's an example of a program that shows how to use distutils.dir_util.copy_tree() to copy a directory in Python:
Code
from distutils import dir_util
def copy_directory(source_dir, destination_dir):
try:
dir_util.copy_tree(source_dir, destination_dir)
print("Directory copied successfully!")
except OSError as e:
print(f"Error occurred while copying directory: {e}")
# Example usage
source_dir = '/path/to/source_directory'
destination_dir = '/path/to/destination_directory'
copy_directory(source_dir, destination_dir)
Output
Directory copied successfully!
Explanation
In this example, the copy_directory() function takes two parameters: source_dir (the path to the source directory) and destination_dir (the path to the destination directory).
The function uses dir_util.copy_tree() to copy the contents of the source directory to the destination directory.
You can replace /path/to/source_directory and /path/to/destination_directory with the actual paths of your source and destination directories. When you run the program, it will attempt to copy the directory and display a success message or an error message if any issues occur.