Table of Contents
In this article, we will see different ways to copy file to another directory in Python.
We can read and write files in Python. We can also work with paths and directories using different libraries.
Ways to copy file to another directoy in Python
We will discuss different methods to copy file from one destination to another.
Using file handling
This method is useful for only small files. We will use a file object to read from a file, and write this to another file.
For example,
1 2 3 |
with open('sample.txt', 'rb') as src, open('Destination/copy.txt', 'wb') as dst: dst.write(src.read()) |
In the above example,
- The
open()
function returns a file object by opening the file at the given path. - The
read()
function reads the content from the file. - The
write()
function writes the data to the destination file.
Using the shutil
library
The shutil
library has many functionalities to perform various operations on files and paths. We can use several methods from this library to copy file to another directory in Python.
First, we have the shutil.copy()
function. It creates a copy of the given source file in the provided destination. It accepts two parameters src
and dst
. Both parameters are assumed to be path-like strings. The destination (dst
) specified can also be a target directory. In that case, the original filename is provided to the copy. It preserves the permissions associated with the file.
For example,
1 2 3 4 |
import shutil shutil.copy('sample.txt', 'Destination/copyfile.txt') |
There is also the shutil.copy2()
function. It performs the same function as the shutil.copy()
method but also preserves the metadata associated with the file.
For example,
1 2 3 4 |
import shutil shutil.copy2('sample.txt', 'Destination/copyfile.txt') |
The shutil.copyfile()
function creates a copy of the given file, but it does not preserve the permissions or the metadata of the file. The destination cannot only be a directory and needs to have the filename and the path.
For example,
1 2 3 4 |
import shutil shutil.copyfile('sample.txt', 'Destinaiton/copyfile.txt') |
The shutil.copyfileobj()
function uses the file handling objects to create copies of the file. It copies the content from the source file object to the destination file object.
For example,
1 2 3 4 5 6 7 8 |
import shutil f1 = open('sample.txt', 'r') f2 = open('Destination/copyfile.txt', 'w') shutil.copyfileobj(f1, f2) f1.close() f2.close() |
In the above example,
- We open the file which we want to copy using the
open()
function in read mode and create the file objectf1
. - We create the
f2
file object using theopen()
function in write mode and open the file where we want to copy. - We copy the contents using the file objects in the
copyfileobj()
function. - We close both the file objects using the
close()
function.
In all the above methods, we get the SameFileError
error if both the source and destination are the same file. It also raises the IOError
if the destination is not writable.
Further reading:
Using the pathlib
library
This method works for Python 3.5 and above, and is similar to the file handling method. Using the pathlib
library, we create two Path
objects with the path of the source file and of the destination file.
Then, we will use the read_bytes()
function to read data from source and write this to the destination file using the write_bytes()
function.
For example,
1 2 3 4 5 6 |
from pathlib import Path src = Path('sample.txt') dst = Path('Destination/copy.txt') dst.write_bytes(src.read_bytes()) |
Using the os
module
The os
module provides a wide range of functionalities to interact and work with the operating system.
The os.popen()
function returns a file object, connected to a pipe. We can run commands using this. For windows, we can use the copy
command and specify the source and destination file to create a copy. The copy
command is used in the command line to create copies of a file. This function will execute this command using Python.
For example,
1 2 3 4 |
import os os.popen('copy sample.txt Destination\copy.txt') |
Similarly, for Linux users, we can use the cp
command in place of copy
.
We can also execute this command in a subshell using the os.system()
function. It will run the command, creating the required copies.
For example,
1 2 3 4 |
import os os.system('copy sample.txt Destination\copy.txt') |
Using the subprocess
module
Similar to the os
module, we can use different functions from the subprocess
module to run the copy
command to copy file to another directory in Python.
The subprocess.call()
function runs the specified command and returns the child return code. We can use it to copy files.
See the code below.
1 2 3 4 |
import subprocess subprocess.call('copy sample.txt Destination\\copy.txt') |
Similarly, we can use the subprocess.check_output()
function to run the copy
command and view the output of this command.
For example,
1 2 3 4 |
import subprocess subprocess.check_output('copy sample.txt Destination\\copy.txt') |
Conclusion
In this tutorial, we discussed different methods from the shutil
, os
, and subprocess
modules to create copies of a file. The os
and subprocess
modules use the copy
command in different functions to create a copy of the file. The shutil
library provides direct functions to create copies of a file and does not use any other command-line command.