A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-shutil-copytree-method/ below:

shutil.copytree() method - Python - GeeksforGeeks

shutil.copytree() method - Python

Last Updated : 12 Jul, 2025

shutil.copytree() method in Python is used to recursively copy an entire directory tree from a source to a destination. It copies all the contents, including files and subdirectories, preserving the directory structure.

Syntax

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)

Parameters:

Return Value: This method returns a string which represents the path of newly created directory.

Examples of shutil.copytree() method Example 1: Using shutil.copytree() method to copy file from source to destination

This code demonstrates how to copy an entire directory, including its contents, from the source path to the destination path using the shutil.copytree() method.

Python
import os
import shutil

# path
path = r'C:\Users\GFG0589\Desktop\vs'

print("Before copying file:")
print(os.listdir(path))

# Source path
src = r'C:\Users\GFG0589\Desktop\vs\source'

# Destination pat
dest = r'C:\Users\GFG0589\Desktop\vs\destination'

# Copy the content of source to destination
destination = shutil.copytree(src, dest)

print("After copying file:")
print(os.listdir(path))

# Print path of newly created file
print("Destination path:", destination)

Output

Before copying file:
['copy_tree_example.py', 'source']
After copying file:
['copy_tree_example.py', 'destination', 'source']
Destination path: C:\Users\GFG0589\Desktop\vs\destination

Explanation: The shutil.copytree() method is used to copy the entire content of the source directory (src) to the destination directory (dest). Theos.listdir() function is called before and after to show the directory contents. The final output prints the path of the newly created directory.

Example 2: Using shutil.copytree() method to copy file by using shutil.copy() method

This example shows how to copy the contents of a directory from the source to the destination using shutil.copytree() with a custom copy function, specifically shutil.copy().

Python
import os 
import shutil 
	
# path 
path = 'C:\Users\GFG0589\Desktop\vs'
	
print("Before copying file:") 
print(os.listdir(path)) 
	
# Source path 
src = 'C:\Users\GFG0589\Desktop\vs\source'
	
# Destination path 
dest = 'C:\Users\GFG0589\Desktop\vs\destination'
	
# Copy the content of source to destination using shutil.copy() as parameter 
destination = shutil.copytree(src, dest, copy_function = shutil.copy) 
	
print("After copying file:") 
print(os.listdir(path)) 
print("Destination path:", destination) 

Output

Before copying file:
['source']
After copying file:
['destination', 'source']
Destination path: C:\Users\GFG0589\Desktop\vs\destination

Explanation: Here, shutil.copytree() is used to copy the content from the source directory to the destination directory. Instead of the default copy2(), shutil.copy() is passed as the copy_function argument to handle the file copying. This method copies the content with basic metadata. The os.listdir() function is used before and after the copy to display changes in the directory, and the destination path is printed at the end.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4