How to Copy File in Python?

Copy file Python

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 module, 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. 

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.

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.

Output:

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.

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.

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.

Output:

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. 

Again, if we pass the same filename as input and the output file, the program will run into the SameFileError as shown below.

Output:

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.

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.

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.

Alternatively, you can use the check_output() function to copy file in python as follows.

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!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *