Python | shutil.copytree() 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.copytree() method recursively copies an entire directory tree rooted at source (src) to the destination directory. The destination directory, named by (dst) must not already exist. It will be created during copying. Permissions and times of directories are copied with copystat() and individual files are copied using shutil.copy2().

Syntax: shutil.copytree(src, dst, symlinks = False, ignore = None, copy_function = copy2, igonre_dangling_symlinks = False)
Parameters:
src: A string representing the path of the source directory.
dest: A string representing the path of the destination.
symlinks (optional) : This parameter accepts True or False, depending on which the metadata of the original links or linked links will be copied to the new tree.
ignore (optional) : If ignore is given, it must be a callable that will receive as its arguments the directory being visited by copytree(), and a list of its contents, as returned by os.listdir().
copy_function (optional): The default value of this parameter is copy2. We can use other copy function like copy() for this parameter.
igonre_dangling_symlinks (optional) : This parameter value when set to True is used to put a silence on the exception raised if the file pointed by the symlink doesn’t exist.
Return Value: This method returns a string which represents the path of newly created directory.



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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain shutil.copytree() method 
       
# importing os module 
import os 
   
# importing shutil module 
import shutil 
   
# path 
path = 'C:/Users / Rajnish / Desktop / GeeksforGeeks'
   
# List files and directories 
# in 'C:/Users / Rajnish / Desktop / GeeksforGeeks' 
print("Before copying file:"
print(os.listdir(path)) 
   
   
# Source path 
src = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / source'
   
# Destination path 
dest = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / destination'
   
# Copy the content of 
# source to destination 
destination = shutil.copytree(src, dest) 
   
# List files and directories 
# in "C:/Users / Rajnish / Desktop / GeeksforGeeks" 
print("After copying file:"
print(os.listdir(path)) 
   
# Print path of newly 
# created file 
print("Destination path:", destination)

chevron_right


Output:

Before copying file:
['source']
After copying file:
['destination', 'source']
Destination path: C:/Users/Rajnish/Desktop/GeeksforGeeks/destination

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain shutil.copytree() method 
        
# importing os module 
import os 
    
# importing shutil module 
import shutil 
    
# path 
path = 'C:/Users / Rajnish / Desktop / GeeksforGeeks'
    
# List files and directories 
# in 'C:/Users / Rajnish / Desktop / GeeksforGeeks' 
print("Before copying file:"
print(os.listdir(path)) 
    
    
# Source path 
src = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / source'
    
# Destination path 
dest = 'C:/Users / Rajnish / Desktop / GeeksforGeeks / destination'
    
# Copy the content of 
# source to destination 
# using shutil.copy() as parameter
destination = shutil.copytree(src, dest, copy_function = shutil.copy) 
    
# List files and directories 
# in "C:/Users / Rajnish / Desktop / GeeksforGeeks" 
print("After copying file:"
print(os.listdir(path)) 
    
# Print path of newly 
# created file 
print("Destination path:", destination)

chevron_right


Output:

Before copying file:
['source']
After copying file:
['destination', 'source']
Destination path: C:/Users/Rajnish/Desktop/GeeksforGeeks/destination


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.