Table of Contents
Python provides us with many modules and functions to perform file operations. In this article, we will discuss different ways to copy file in python. For this, we will use the os module, shutil modul
e, and subprocess module
. We will also discuss advantages and disadvantages for each approach as file operations are costly in terms of execution time and a wrong approach can become a bottleneck for the performance of our program.
Copy file using the shutil module in Python
The shutil module provides us with optimized functions to copy files in python. For this, we have four functions namely copy()
, copyfileobj()
,copy2()
, and copyfile()
function. Let us discuss each function separately.
Copy file using the copyfileobj() function in Python
The copyfileobj()
function is used to copy the contents of a file to another file by explicitly opening the file.
To copy file in python using the copyfileobj()
, we first need to open the source file and the destination file using the open()
function. The open()
function takes the file name as the first argument and the mode to open the file as the second argument and returns a file object. We will open the source file in the “rb
” mode i.e. we will read the file in bytes and not the actual content of the file. Similarly, we will open the destination file in the “wb
” mode.
After opening both the files, we will copy the data from the source file to the destination file using the copyfileobj()
function. The copyfileobj()
takes the source file object as its first input argument and the destination file object as its second input argument. Upon execution, it copies the contents from the source file to the destination file using a buffer of 16KB. To change the size of the buffer, we can use a third optional argument namely buffer size to change the size of the buffer.
1 2 3 4 5 6 7 8 9 10 |
import shutil filename1 = 'sample.txt' myFile1 = open(filename1, 'rb') filename2 = 'output_file.txt' myFile2 = open(filename2, 'wb') shutil.copyfileobj(myFile1, myFile2) myFile1.close() myFile2.close() |
As we have explicitly opened the files in the above example, we need to close the file objects using the close()
function. Otherwise, the data won’t be saved in the destination file.
When we copy file using this approach, no metadata or permission of the source file is copied to the destination file.
Copy file using the copyfile() function in Python
Instead of explicitly opening the file, copying the content using the copyfileobj()
function, and closing the file, we can use the copyfile()
function to copy file in python.
The copyfile()
function takes the source file name and the destination file as the first and the second input arguments respectively. Upon execution, it copies the contents of the source file to the destination file as shown below.
1 2 3 4 5 6 7 |
import shutil filename1 = 'sample.txt' filename2 = '/home/aditya1117/output_file.txt' shutil.copyfile(filename1, filename2) |
Here, the source file must be readable and the source and destination address must be different. If the source and the destination file addresses are the same, the copyfile()
function will raise shutil.SameFileError
exception with the message “shutil.SameFileError: 'sample.txt' and 'sample.txt' are the same file
” as shown below.
1 2 3 4 5 6 7 8 |
import shutil filename1 = 'sample.txt' filename2 = 'sample.txt' shutil.copyfile(filename1, filename2) |
Output:
1 2 3 4 5 6 7 8 |
Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 5, in <module> shutil.copyfile(filename1, filename2) File "/usr/lib/python3.8/shutil.py", line 244, in copyfile raise SameFileError("{!r} and {!r} are the same file".format(src, dst)) shutil.SameFileError: 'sample.txt' and 'sample.txt' are the same file |
The copyfile()
function uses the copyfileobj()
function in its implementation. It doesn’t have many benefits over the copyfileobj()
function in terms of execution. But, it makes our task easier as we don’t have to explicitly perform all the file operations. Also, the copyfile()
function copies no metadata or permission of the source file into the destination file.
For copying metadata and file permissions, we can use the copy()
and the copy2()
functions as discussed ahead.
Copy file using the copy() function in Python
The copy()
function defined in the shutil
module works as the cp
command in Linux or copy
command in windows. The function takes two strings as input arguments. The first input argument should contain the address of the file that needs to be copied. The file address can be an absolute or relative file path. The second input argument denotes the file address to which the input file should be copied. After execution of the function, the source file content is copied to the destination file. You can observe this in the following example.
1 2 3 4 5 6 7 |
import shutil filename1 = 'sample.txt' filename2 = '/home/aditya1117/output_file.txt' shutil.copy(filename1, filename2) |
If we pass a directory instead of the file name as the second input argument, the copy() function creates a file with the same name as the source file. For instance, look at the following example.
1 2 3 4 5 6 7 |
import shutil filename1 = 'sample.txt' filename2 = '/home/aditya1117/' shutil.copy(filename1, filename2) |
Here, we have passed a directory named /home/aditya1117/
as the second input argument to the copy()
function. After execution, the copy()
function will create a file name with the same name as the source file i.e sample.txt
in the given directory. On the other hand, We had explicitly provided the file name of the copied file in the previous example and the resultant file was created with the provided name.
Also, if we pass the same file address as the source and destination arguments, the copy()
function raises the SameFileError
denoting that it cannot copy file’s contents to the same file. You can observe this in the following example.
1 2 3 4 5 6 7 |
import shutil filename1 = 'sample.txt' filename2 = 'sample.txt' shutil.copy2(filename1, filename2) |
Output:
1 2 3 4 5 6 7 8 9 10 |
Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 5, in <module> shutil.copy2(filename1, filename2) File "/usr/lib/python3.8/shutil.py", line 435, in copy2 copyfile(src, dst, follow_symlinks=follow_symlinks) File "/usr/lib/python3.8/shutil.py", line 244, in copyfile raise SameFileError("{!r} and {!r} are the same file".format(src, dst)) shutil.SameFileError: 'sample.txt' and 'sample.txt' are the same file |
You might be amazed to know that the copy()
function uses the copyfile()
function to copy the file contents. Additionally, it uses the copymode()
function to copy the file permission from the source file to the destination file. The copymode()
function takes the source file name and the destination file name and copies all the file permissions from the source file to the destination file.
So, we can observe that the copyfileobj()
function and the copyfile()
function do not copy the file permissions from the source file to the destination file whereas the copy()
function copies the permissions as well.
Copy file using the copy2() function in Python
The copy()
function only copies the permission of the source file to the destination file. However, the copy2()
function also copies the metadata of the source file to the destination file. The copy2()
function works in the same way as the copy()
function. Along with the copyfile()
function, it executes the copystat()
function that copies the metadata as well as the file permissions of the source file to the destination file.
1 2 3 4 5 6 7 |
import shutil filename1 = 'sample.txt' filename2 = '/home/aditya1117/output_file.txt' shutil.copy2(filename1, filename2) |
Again, if we pass the same filename as input and the output file, the program will run into the SameFileError
as shown below.
1 2 3 4 5 6 7 8 |
import shutil filename1 = 'sample.txt' filename2 = 'sample.txt' shutil.copy2(filename1, filename2) |
Output:
1 2 3 4 5 6 7 8 9 10 |
Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 5, in <module> shutil.copy2(filename1, filename2) File "/usr/lib/python3.8/shutil.py", line 435, in copy2 copyfile(src, dst, follow_symlinks=follow_symlinks) File "/usr/lib/python3.8/shutil.py", line 244, in copyfile raise SameFileError("{!r} and {!r} are the same file".format(src, dst)) shutil.SameFileError: 'sample.txt' and 'sample.txt' are the same file |
Till now we have discussed four approaches to copy file in python using the shutil
module. Out of these, the copy2()
function offers the most functionalities while the approach with the copyfileobj()
is the most naive one. Let us now discuss ways to copy file using the operating system commands in python.
Copy file using the operating system command in Python
With the os module
or the subprocess module
in python, we can execute the operating system commands that are generally used in the command line terminal. To copy file using the operating system commands, we will use the cp
command in Linux. The syntax of the cp
command is as follows.
cp source_address destination_address
Upon execution of the command, the file at the source_address
will be copied to the destination_address
.
In the windows operating system, you can use the following command to copy file from the source_address
to the destination_address
.
copy source_address destination_address
Copy file using the os module in Python
In python, we can execute the operating system commands using the system()
function defined in the os
module. The system function takes a string containing the operating system command as an input argument and executes the command. Internally, the system()
function uses the standard library functions defined in the C language to copy the file.
1 2 3 4 5 6 |
import os os.system("cp sample.txt output.txt") |
We can also use the popen()
function defined in the os
module to execute the operating system command to copy file in python as follows.
1 2 3 4 |
import os os.popen("cp sample.txt output.txt") |
Copy file using the subprocess module in Python
Instead of the os
module, we can also use the subprocess
module to copy file using the operating system commands. For this, we can use the call()
function. The call()
function takes the string containing the operating system command and returns 0 after successful execution of the command.
1 2 3 4 5 6 |
import subprocess subprocess.call("cp sample.txt output.txt", shell=True) |
Alternatively, you can use the check_output()
function to copy file in python as follows.
1 2 3 4 5 |
import subprocess subprocess.check_output("cp sample.txt output.txt", shell=True) |
Conclusion
In this article, we have discussed different ways to copy file in python. You can any of the approaches to copy file. However, I would suggest that you use the copy()
or copy2()
function defined in the shutil
module as they are explicitly designed for copying files.
That’s all about how to copy file in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!