Python Copy File

Python Copy File: In Programming, many times we have to deal with the file and perform Input/Output operations in it as well.

One of the important File Operation is Copy File Operation.

In Copy File Operation, the content of one file(source) is copied to another file(destination).

It can be challenging to perform Copy File Operation in some Programming Languages.

However, In Python you can easily do so with the help of different modules available which makes the copy file operation very easy and fun as well.

This is the another reason for Python being among the Top Programming Languages as well.

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.

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.

from shutil import copy
from os import listdir

path = "I:\Files"
source_file = "I:\Files\words.txt"
destination_file = "I:\Files\words(copy).txt"

print("\nBefore performing Copy File Operation:")
print(listdir(path))

copy(source_file, destination_file)

print("\nAfter performing Copy File Operation:")
print(listdir(path))

OUTPUT:

Python Copy File with copy()

You can see in the destination_file, we have specified the destination file name as well.

Now, let’s see the code to copy a source file to the destination directory. Here we will give destination directory name instead of file name.

from shutil import copy
from os import listdir

path = "I:\Words"
source_file = "I:\Files\words.txt"
destination_file = "I:\Words"

print("\nBefore performing Copy File Operation:")
print(listdir(path))

copy(source_file, destination_file)

print("\nAfter performing Copy File Operation:")
print(listdir(path))

OUTPUT:

copy() method

2. shutil.copyfile()​

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.

from shutil import copyfile
from os import listdir

path = "I:\Files"
source_file = "I:\Files\words.txt"
destination_file = "I:\Files\words(copy).txt"

print("\nBefore performing Copy File Operation:")
print(listdir(path))

copyfile(source_file, destination_file)

print("\nAfter performing Copy File Operation:")
print(listdir(path))

OUTPUT:

Python Copy File with copyfile()

3. shutil.copystat()​

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)

So, if you want to copy the information like – Metadata Information, File Permission and Modification Time as well, then you should make use of shutil.copystat() method.

Firstly, you need to copy the content by using shutil.copy() and then you need to use shutil.copystat() method to copy the Metadata and other related information.

Let’s take a look at a code to perform Copy File Operation with shutil.copystat() method.

from shutil import copystat
from shutil import copy
from os import listdir

path = "I:\Files"
source_file = "I:\Files\words.txt"
destination_file = "I:\Files\words(copy).txt"

print("\nBefore performing Copy File Operation:")
print(listdir(path))

copy(source_file, destination_file)

copystat(source_file, destination_file)

print("\nAfter performing Copy File Operation:")
print(listdir(path))

OUTPUT:

copystat() method

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.copy2(source_file, destination)

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.

from shutil import copy2
from os import listdir

path = "I:\Files"
source_file = "I:\Files\words.txt"
destination_file = "I:\Files\words(copy).txt"

print("\nBefore performing Copy File Operation:")
print(listdir(path))

copy2(source_file, destination_file)

print("\nAfter performing Copy File Operation:")
print(listdir(path))

OUTPUT:

copy2() method

Exception Handling with Python Copy File​

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:

Exception Handling with Python Copy File

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.

If you want the regular updates and tips related to Programming and Software Development, right in your Inbox, Subscribe to us by filling the form below and stay connected with us.