Open In App

Python | shutil.copyfile() method

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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() method?

The shutil.copyfile() method in Python is used to copy the content of the source file to the destination file. The metadata of the file is not copied. The source and destination must represent a file and the destination must be writable. If the destination already exists then it will be replaced with the source file otherwise a new file will be created. 

If the source and destination represent the same file then the SameFileError exception will be raised. 

shutil.copyfile() Method Syntax 

shutil.copyfile(source, destination, *, follow_symlinks = True)

Parameters: 

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

Note: The ‘*’ in the parameter list indicates that all following parameters (Here in our case ‘follow_symlinks’) are keyword-only parameters and they can be provided using their name, not as positional parameters.

Returns: 

This method returns a string that represents the path of the newly created file.

How to Copy a File using Shutil.copyfile() Method in Python

Using shutil.copyfile() method you can easily copy a file to a new file. To use this method just need to mention the source file location and destination file location. 

Let’s understand it better with an example:

Use of shutil.copyfile() method to copy a file from source to destination 

Here in this example we will see the use of shutil.copyfile() on how to copy a file from source to destination using Python.

Python3




# Python program to explain shutil.copyfile() method
 
# importing os module
import os
 
# importing shutil module
import shutil
 
# path
path = '/home/User/Documents'
 
# List files and directories
# in '/home/User/Documents'
print("Before copying file:")
print(os.listdir(path))
 
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents/file(copy).txt"
 
# Copy the content of
# source to destination
dest = shutil.copyfile(source, destination)
 
# List files and directories
# in "/home / User / Documents"
print("After copying file:")
print(os.listdir(path))
 
# Print path of newly
# created file
print("Destination path:", dest)


Output:

 Before copying file:
['hrithik.png', 'test.py', 'sample.txt', 'file.text', 'copy.cpp']
After copying file:
['hrithik.png', 'test.py', 'sample.txt', 'file.text', 'file(copy).txt', 'copy.cpp']
Destination path: /home/User/Documents/file(copy).txt

Possible Errors

Here we seeing Possible errors occur while using shutil.copyfile() method.

If the source and destination represent the same file ‘SameFileError’ exception will be raised If the destination is a directory then ‘IsADirectoryError’ exception will be raised If the destination is not writable ‘PermissionError’ exception will be raised

Python3




# Python program to explain shutil.copyfile() 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
shutil.copyfile(source, destination)


Output:

Traceback (most recent call last):
File "copy.py", line 31, in
shutil.copyfile(source, destination)
File "/usr/lib/python3.6/shutil.py", line 104, in copyfile
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
shutil.SameFileError: '/home/User/Documents/file.txt' and '/home/User/Documents/file.txt'
are the same file

Destination path: /home/User/Documents/file(copy).txt

Error Handling

Here we are Handling errors while using shutil.copyfile() method.

Python3




# Python program to explain shutil.copyfile() method
 
# importing shutil module
import shutil
 
# Source path
source = "/home/User/Documents/file.txt"
 
# Destination path
destination = "/home/User/Documents"
 
# Copy the content of
# source to destination
 
try:
    shutil.copyfile(source, destination)
    print("File copied successfully.")
 
# If source and destination are same
except shutil.SameFileError:
    print("Source and destination represents the same file.")
 
# If destination is a directory.
except IsADirectoryError:
    print("Destination is a directory.")
 
# If there is any permission issue
except PermissionError:
    print("Permission denied.")
 
# For other errors
except:
    print("Error occurred while copying file.")


Output:

Destination is a directory.

We have covered the method of copying a file in Python using shutil.copyfile() function. It is a very easy-to-use method that you can use to copy a file. It is very useful when you want to create a duplicate file.

Also Read:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads