You may need to copy a file in a number of programs. Imagine if you wanted to build a logging function for your program that begins with a predefined set of text. You could create a copy of an existing blank log file for each log that you want to create.
The Python shutil library comes with a number of functions for copying files. In this guide, we talk about how to use the shutil.copy()
and the shutil.copy2()
methods to copy a file using the Python programming language. Let’s begin.
Python Copy File
The Python shutil library, which allows you to manipulate files and directories, contains methods for copying files. The copy()
and copy2()
methods are commonly used because they let you copy the permissions associated with an existing file.
The difference between copy()
and copy2()
is that the former does not copy the metadata associated with files (the pieces of information about a file, like who created the file) whereas the latter does copy this information.
Let’s discuss how to use each of these two methods.
Python shutil.copy()
Let’s build a program that generates report cards for each student at a school. To start, we want to create files for each student in our class. The files should begin with:
-------------------------------------------------- Career Karma School Report Card -------------------------------------------------- START OF STUDENT TRANSCRIPT --------------------------------------------------
This information is already in a file called template.txt. We want to copy this template to create a file for each student in our school. To start, let’s import the shutil library which we will use to copy our files and then define a list of students:
import shutil students = ["Lucy", "Peter", "Chad"]
We’re going to iterate over this list and create a blank transcript for each student:
for s in students: shutil.copy("/home/james/students/template.txt", "/home/james/students/data/{}.txt".format(s)) print("{}.txt has been created for {}'s report card.".format(s, s))
This loop runs through every student in our list. We copy the template.txt file and paste that file into the /home/james/students/data/ for each student. The file for each student has the name:
[Student name].txt
The [Student name] value represents the name of a student.
Let’s run our program and see what happens:
Lucy.txt has been created for Lucy's report card. Peter.txt has been created for Peter's report card. Chad.txt has been created for Chad's report card.
Our code displays three messages which indicate each student’s report card has been created.
If we look at the /home/james/students/data/ folder, we can see three files:
Lucy.txt has been created for Lucy's report card. Peter.txt has been created for Peter's report card. Chad.txt has been created for Chad's report card.
Each file contains the text from our earlier template.
Python shutil.copy2() Method
The shutil.copy2()
method copies a file from one place to another on your operating system. This method, unlike shutil.copy()
, also copies the metadata associated with a file. shutil.copy2()
uses the same syntax as the shutil.copy()
method.
Suppose we want to create a progress certificate for someone playing a game. This certificate should begin with the following text:
RPG: User score card Produced by the RPG Python game
This text is already in the file default_scorecard.txt. We could copy the default_scorecard.txt file using the copy2()
method in the shutil library:
import shutil shutil.copy("/home/james/game/default_scorecard.txt". "/home/james/game/recent_scores.txt") print("Your progress has been recorded in the recent_scores.txt file.")
We import the shutil library and then we use the shutil.copy2()
method to create a copy of the default_scorecard.txt file. We then print a message to the console informing us that the original file has been copied into the recent_scores.txt file.
Other Methods
You can also use the copyfile()
and copyfileobj()
methods in the shutil library.
Both of these methods use the same syntax as the two methods we discussed earlier:
shutil.copyfile(source, destination) shutil.copyfileobj(source, destination)
The copyfile()
method lets you copy a file from one location to another. You can also instruct Python to create a symlink between the new and the old file. The copyfileobj()
method lets you copy a file and specify how the data is read in chunks.
You can learn more about these methods by reading the Python shutil documentation.
Conclusion
The Python shutil.copy()
and shutil.copy2()
methods allow you to copy a file to another location. These methods copy permissions attached to an existing file. You can also use the copyfile()
and copyfileobj()
methods in the shutil library to copy a file.
Do you want to learn more about coding in Python? Read our How to Learn Python guide. This guide comes with a range of tips to help you learn Python coding. You will also find learning resources to guide your learning.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.