Copying Files to another directory

shutil.copy() method in Python is used to copy the content of the source file to the destination file or directory. It also preserves the file’s permission mode but other metadata of the file like the file’s creation and modification times is not preserved.
The source must represent a file but the destination can be a file or a directory. If the destination is a directory then the file will be copied into the destination using the base filename from the source. Also, the destination must be writable. If the destination is a file and already exists then it will be replaced with the source file otherwise a new file will be created.

Syntax: shutil.copy(source, destination, *, follow_symlinks = True)

Parameter:

  • source: A string representing the path of the source file.
  • destination: A string representing the path of the destination file or directory.
  • follow_symlinks (optional) : The default value of this parameter is True. If it is False and source represents a symbolic link then destination will be created as a symbolic link.

Return Type: This method returns a string which represents the path of newly created file.

Example 1:

Python3




# Python program to explain shutil.copy() method
 
# importing shutil module
import shutil
 
source = "path/main.py"
destination ="path/main2.py"
 
# Copy the content of
# source to destination
dest = shutil.copy(source, destination)
 
# Print path of newly
# created file
print("Destination path:", dest)


Output:

Destination path: path/main2.py

Example 2: If the destination is a directory.

Python3




# importing shutil module 
import shutil
   
# Source path
source = "path/main.py"
   
# Destination path
destination = "path/gfg/"
   
# Copy the content of
# source to destination
dest = shutil.copy(source, destination)
   
 
# Print path of newly 
# created file
print("Destination path:", dest)


Output:

path/gfg/main.py

Shutil Module in Python

Shutil module offers high-level operation on a file like a copy, create, and remote operation on the file. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories. In this article, we will learn this module.

Similar Reads

Copying Files to another directory

shutil.copy() method in Python is used to copy the content of the source file to the destination file or directory. It also preserves the file’s permission mode but other metadata of the file like the file’s creation and modification times is not preserved.The source must represent a file but the destination can be a file or a directory. If the destination is a directory then the file will be copied into the destination using the base filename from the source. Also, the destination must be writable. If the destination is a file and already exists then it will be replaced with the source file otherwise a new file will be created....

Copying the Metadata along with File

...

Copying the content of one file to another

...

Replicating complete Directory

shutil.copy2() method in Python is used to copy the content of the source file to the destination file or directory. This method is identical to shutil.copy() method but it also tries to preserve the file’s metadata....

Removing a Directory

...

Finding files

...