Python

Python Shutil Copy

Beginner developers have plenty of questions in their minds when they start coding in any programming language. Whenever they learn about a new module, function, or library, the first thing that comes up in their minds is how it works. You’re probably wondering what the shutil.copy() module is and what it does. Well, this article will get you familiar with the shutil.copy() module of the Python programming language. We will help you understand its functionality with the help of precise and easy-to-understand examples. Let us begin learning.

The shutil is a python programming language module that provides several high-level operational functions on files. It is a standard utility module of the Python programming language. It helps automate the process of copy-pasting and deleting files and directories. The shutil.copy() is a method of the shutil module in python. It helps copy the data from a source file and paste it to the destination file. It preserves the permission mode of a file; however, the other metadata, like the creation or modification time, is not preserved.

Moreover, there are some conditions with the shutil.copy() method that the user needs to keep in mind while working with the method. When you copy the data using shutil.copy() method, the source file should represent a file; but the destination file could either be a file or a directory to paste the data. The destination, on the other hand, must be readable. When the destination seems to be a directory, the source file is transferred from the source location to the target directory with the base name. If you are pasting the data in a file, then a new file is created; however, it will be replaced if the file already exists. The syntax of shutil.copy() method is as follow:

# shutil.copy(source_file, destination,*, follow_link = True)

The parameter source_file in shutil.copy() method represents the path of the source file, the destination parameter represents the destination file’s or directory’s path. The ‘*’ parameter indicates that the following parameters are only keyword parameters, not positional parameters, and can simply be provided by their name. The follow_link parameter is optional, and its value is TRUE. When its value is FALSE, but the source_file has a symbolic_link, the destination will be created as a symbolic_link.  It produces a string containing the newly created file’s path. Below are some examples we have provided to help you understand the syntax and usage of shutil.copy() method of shutil module in python. Let us work with the examples given below.

Example 1:

Here we will copy a file from source to destination using shutil.copy(). By practicing the example given below, you will learn how to copy and paste data from a source file to a destination file using shutil.copy() method. Please look at the code below and incorporate it into your own program.

Text Description automatically generated

The following is the result of the aforementioned code. However, the output will vary when you run the same code in your system. The output shows the list of items in the directory of the system on which the program is executed. Hence, when you run it in your system and provide your customized path, you list items in that directory. See the output given below only for reference.

Text Description automatically generated

Example 2:

This example is about how to copy a file when the source is a file and destination is a directory using shutil.copy(). As previously stated, the destination could have been a directory; nevertheless, the source ought to be a file. Hence, we have created a customized program to help you understand how to copy the data when the source is a file, and the destination is a directory using shutil.copy(). Let’s take a look at the following piece of code:

Text Description automatically generated

The above code will copy the source file and paste it into the destination path. Here you can see the result of the above code.

Text Description automatically generated

Example 3:

Now we will discuss how you can handle the errors while working with the shutil.copy() method. When you start making a program, there is a probability of errors. Hence, knowing how to deal with those errors and how to resolve those errors is very important. This example will guide you on how to handle the error you may encounter while using shutil.copy() method. See the code below:

Text Description automatically generated

Note that how to try: and except is used to handle the error which may occur while using the shutil.copy() method.

Conclusion:

Shutil is a concept that we studied in this article. With the help of three simple and easy-to-understand examples, we have learned how to use shutil.copy() method in different situations and how to handle the errors which may be encountered while using shutil.copy() method.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content