Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | shutil.copyfile() method
Next article icon

shutil.copytree() method – Python

Last Updated : 08 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  • src: The path to the source directory to be copied.
  • dst: The path to the destination directory where the contents will be copied.
  • symlinks (optional): If True, it copies symbolic links as symbolic links. Default is False.
  • ignore (optional): A callable function that can be used to ignore certain files. It should take two arguments: the directory path and the list of file names in the directory.
  • copy_function (optional): A function to use for copying files. The default is shutil.copy2(), which preserves file metadata.
  • ignore_dangling_symlinks (optional): If True, it prevents errors when copying broken symlinks. Default is False.

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.



Next Article
Python | shutil.copyfile() method

R

Rajnis09
Improve
Article Tags :
  • Python
  • Python-shutil
Practice Tags :
  • python

Similar Reads

  • Python | shutil.copy() method
    C/C++ Code # Python program to explain shutil.copy() method # importing shutil module import shutil # Source path source = "/home/User/Documents/file.txt" # Destination path destination = "/home/User/Documents/file.txt" # Copy the content of # source to destination try: shutil.co
    5 min read
  • Python | shutil.copy2() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories.shutil.copy2() method in Python is used to copy the co
    4 min read
  • Python | shutil.copyfile() method
    Shutil module in Python helps automate the process of copying and removing files and directories. It comes under Python’s standard utility modules. Shutil(short for shell utility) module also provides many functions of high-level operations on files and collections of files. What is Shutil.copyfile(
    4 min read
  • Python | shutil.copystat() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories.shutil.copystat() method in Python is used to copy the
    3 min read
  • Python | shutil.copymode() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories.shutil.copymode() method in Python is used to copy
    2 min read
  • Python | shutil.copyfileobj() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of chowning and removal of files and directories.shutil.copyfileobj() method in Python is used to copy
    2 min read
  • Python | shutil.move() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removing files and directories. The shutil.move() method moves a file or directory
    2 min read
  • Python List copy() Method
    The copy() method in Python is used to create a shallow copy of a list. This means that the method creates a new list containing the same elements as the original list but maintains its own identity in memory. It's useful when you want to ensure that changes to the new list do not affect the origina
    3 min read
  • Python | Decimal copy_sign() method
    Decimal#copy_sign() : copy_sign() is a Decimal class method which returns the copy of the first Decimal value with the sign set to be the same as the sign of the second Decimal value. Syntax: Decimal.copy_sign() Parameter: Decimal values Return: the copy of the first Decimal value with the sign set
    2 min read
  • Python PIL | copy() method
    PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.copy() method copies the image to another image object, this method is useful when we need to copy the image but also retain the original. Syntax:Image.copy() Parameters: no arguments R
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences