There are many ways to perform a Copy File Operation in Python. However, here we are going to discuss the shutil module used for Copy File Operation in Python.
Table of Contents
Python Copy File using shutil module
So, let’s take a look at the top 4 Python Copy File methods:
1. shutil.copy()
This method is used to copy the content of a source file to destination file or directory.
So, here the destination can be a file or a directory.
The syntax of using shutil.copy() is:
shutil.copy(source_file, destination)
If the destination is a file, then it will copy the content of source file to that specified destination file.
However, if the destination is a directory, then it will copy the source file and create the same file with the same name in destination directory.
If the destination is a directory, make sure that the source address and destination must be different, other wise it will give SameFileError exception.
With shutil.copy() method, you cannot copy the associated information , like – metadata and file permission.
So, keeping the above point in mind, let’s take a look at a code to implement Copy File Operation with shutil.copy() method.
We will firstly see the code to copy the content of source file to the destination file.
If you want to copy the content of one file(Source) to another file(Destination), then you can make use of shutil.copyfile() method.
However, you cannot copy the metadata of file using this method.
The basic syntax of copyfile() is as follows:
shutil.copy(source_file, destination_file)
Make sure that the source_file and destination_file must target to file. For example – In destination_file, if you target a directory instead of file, then it will return Error 13.
Also make sure that the destination_file have a write permission, otherwise it will give IOError exception.
Another thing you should keep in mind is that source_file and destination_file must be different. Otherwise it will give SameFileError exception.
A new file will be created if destination directory doesn’t have a specified file in it.
However, if the destination directory already have a file with same name as specified in destination_file. Then the content of source file will get override into the destination file.
So, keeping the above points in mind that take a look at the code to perform Copy File Operation in Python.
The above two Copy File methods we have discuss are used to only copy the content of a source to the destination.
However those methods doesn’t copy the complete information of a file, like – Metadata Information, File Permission and Modification Time.
With shutil.copystat() method you can copy the Metadata and other associated information of the source file.
The syntax to implement shutil.copystat() is:
shutil.copystat(source_file, destination_file)
Output:
4. shutil.copy2()
This method is like the upgraded version of sutil.copy() method.
With shutil.copy2() method, the content of source file get copied in destination. Along with it the Metadata and other information of a source file also get copied.
Syntax to perform Copy File Operation with shutil.copy2() is as follows:
shutil.copystat(source_file, destination_file)
shutil.copy2() directly copy the content as well other related information on to the destination.
The code to perform Copy File Operation using shutil.copy2() is as follow.
There can be number of exceptions occur while using the above discussed modules for performing Copy File operation.
For example – IOError exception, PermissionError exception or IsADirectoryError exception.
So, let’s take a look at how to successfully deal with these Errors.
from shutil import copyfile
from os import listdir
path = "I:\Files"
source_file = "I:\Files\words.txt"
destination_file = "I:\Files"
try:
print("\nBefore performing Copy File Operation:")
print(listdir(path))
copyfile(source_file, destination_file)
print("\nAfter performing Copy File Operation:")
print(listdir(path))
except IsADirectoryError:
print("Destination File is Directory")
except IOError:
print("Input Output operation get Failed")
except PermissionError:
print("Don't have the Permission to Copy a file")
except:
print("Unexpected error occured")
Output:
So, hope you like the article on Python Copy File in which you have seen the different methods for performing a Copy Operation in Python and how to deal with the exceptions as well.