Working with Python may require that you programmatically copy a file at some point. And this is particularly helpful when you’re creating a backup. In addition, one common way to copy a file from one directory to another in Python is by using a library known as shutil.
Shutil, short for shell utility, is a module in Python’s Standard Library, which allows you to manipulate directories and files, alongside directory functions. This library has various methods with which you can copy a file.
But this depends on whether you want to copy file permissions or metadata, and if the specific destination will be a directory. In this article, we’ll look at the methods for copying files with Python. And you’ll understand how to do this using the shutil module.
Table of Contents
Copy a File With Python Using The Shutil.Copyfile() Method
With the Shutil.Copyfile() method, you can copy the source file to the specified destination file without the metadata. In other words, this file copying method with Python can be used to copy a file’s content into another file.
Here, the src could be a string or a path-like object. Check out this example on how to use the shutil.copyfile() method.
# import module
import shutil
# copy the contents of the demo.py file to a new file called demo1.py
shutil.copyfile(‘./demo.py’, ‘./demo1.py’)
Firstly, use the import shutil statement to import the module. Afterward, use the shutil.copyfile() method whose syntax is as follows:
shutil.copyfile(‘source_file’, ‘destination_file’)
Note:
- The path to the file you’d want to copy is the source_file. Going by our example, that file is the demo.py file in your present working directory (./).
- The path to the new file you want to create is the destination_file. Following our example, you should copy the source file’s contents into a new file — demo.py, in your working directory. Meanwhile, the destination must be a file name, not a directory.
Once you run the code in the above example, it creates a new file name (demo1.py) in your present working directory. This new file name contains a copy of demo.py’s contents. But, it will be replaced if the destination file already exists.
Remember, no file permissions or metadata are copied to the specified destination file. Additionally, you can use this Python file copy method to rename the file you’re copying without bothering about storing file permissions and metadata.
How To Copy A File Using Shutil.Copy() Method In Python
Using the shutil.copy() method, you can copy a file to another directory in Python. In Python, you can use this method to copy a specified source (with no metadata) to the directory or destination file, and return the path to the new file that was just created.
Meanwhile, the src could be a string or a path-like object. Here’s an example:
# import the module
import shutil
# Specify the path of the file you want to copy
file_to_copy = ‘./demo.py’
# Specify the path of the destination directory you want to copy to
destination_directory = ‘./projects’
# Use the shutil.copy() method to copy the file to the destination directory
shutil.copy(file_to_copy, destination_directory)
Firstly, use the import shutil statement to import the module. Next, specify the file’s path that you want to copy and save it in this variable: file_to_copy. From this example, we’re copying the demo.py file in our working directory.
Afterward, ensure you specify the directory you’d want to copy the file to. Then save it in a destination_directory variable. In this context, we’re saving the file in the projects directory of our working directory.
Finally, use the shutil.copy() method with two arguments:
- The path of the file you intend copying. In this context, we’re copying the file_to_copy variable.
- Then, the directory or file you’re copying the file into. In this context, it’s the destination_directory variable.
Now, the shutil.copy() method will make a copy of the demo.py file in the projects directory once you run the code in our example above.
Remember, if there’s a file in the destination directory with the same name, the new file will overwrite it. In addition, this method copies file permissions.
Copying A File Using The Shutil.Copy2() Method In Python
The shutil.copy2() method of copying a file in Python is similar to the shutil.copy() method. However, what differentiates them is that the shutil.copy2() preserves file metadata when copying.
Furthermore, it does not function with file objects and copies metadata. Again, its destination can be a directory, and it preserves file permissions. Check out this example below:
# import the module
import shutil
# Specify the path of the file you want to copy
file_to_copy = ‘./demo.py’
# Specify the path of the destination directory you want to copy the file into
destination_directory = ‘./projects’
# Use the shutil.copy2() method to copy the file to the destination directory
shutil.copy2(file_to_copy, destination_directory)
How To Copy A File Using The Shutil.Copyfileobj() Method In Python
Are you working with file objects? If yes, the shutil.copyfileobj() is what you should use. In this method, the contents of the source file object will be copied to the specified destination file object. Additionally, you can set up the length that matches the buffer size you used to copy the contents.
The shutil.copyfileobj() method takes two arguments: a destination file object and a source file object. And the destination can’t be a directory. Let’s look at this example:
# import module
import shutil
# you have to open the source file in binary mode with ‘rb’
source_file = open(‘demo.py’, ‘rb’)
# you have to open the destination file in binary mode with ‘wb’
destination_file = open(‘project.py’, ‘wb’)
# use the shutil.copyobj() method to copy the contents of source_file to destination_file
shutil.copyfileobj(source_file, destination_file)
From our example, the shutil.copyobj() copies the demo.py’s contents to the project.py file. Note that this method does not copy metadata and it doesn’t preserve file permissions.
Check Out: Introduction To Priority Queues in Python
How To Choose The Right Method To Copy a File With Python
We have looked at the various ways you can use the shutil module to copy file’s with Python. But the big question is this: how can you choose the right method for copying a file with Python?
Your specific use-case will determine the right method to choose. So, ask yourself these questions:
- Do you want to copy something that has file objects?
- Secondly, do you want to copy a file to a directory?
- Finally, do you want to copy the metadata or file permissions?
Your answers to these questions will guide you in choosing the right method for your specific use-case. Meanwhile, the table below is a summary of each method’s capabilities.
Method | Preserves Permissions | Copies Metadata | Destination Can Be a Directory | Accepts File Object |
shutil.copyfile() | X | X | X | X |
shutil.copy() | ✓ | X | ✓ | X |
shutil.copy2() | ✓ | ✓ | ✓ | X |
shutil.copyfileobj() | X | X | X | ✓ |
Conclusion
With the shutil module, you can easily copy a file with Python from one directory to another. You can achieve this by using any of the four functions/methods discussed in this article. However, the method you use depends on the specific use-case.
This functionality can be useful for scenarios like creating several versions of a file for testing purposes, backing up essential data, or automating file management tasks. With some lines of Python code, you can boost your productivity and narrow your file operations.