How to Copy a File Using Python?

Python provides various functions to perform different operations on the files, such as create, edit, remove, modify, copy, etc. One such module used to perform high-end operations on a single or collection of files in Python is the “Shutil” module. 

This article will discuss the possible ways to copy a file using Python: 

Method 1: Using “shutil.copyfile()” Function

The “shutil.copyfile()” function is used in Python to copy files between directories without copying the metadata of the file. The metadata represents the extra information related to the specified file. 

The below snippet shows the “sample.txt” file’s original path.

In the example code given below, the “shutil.copyfile()” function is used to copy a file:

Code:

import shutil
original_path = r'C:/Users/p/Documents/ILF/sample.txt'
target_path = r'C:/Users/p/Documents/program/sample.txt'
shutil.copyfile(original_path, target_path)

In the above code, the “shutil.copyfile()” accepts the original path as a first argument and the destination path as a second argument.

The snippet below shows the file’s new path named “sample.txt” 

Method 2: Using “shutil.copy()” Function

The “shutil.copy()” function is used to copy the source file to the targeted file along with file permission mode. But the other metadata information is not copied in this method. In the example given below, the “shutil.copy()” function is used to copy the file:

Code:

import shutil
shutil.copy('sample.txt', 'C:/Users/p/Documents/ILf/sample_1.txt')

In the above code, the “shutil.copy()” function takes the file name with the format of file such as “sample.txt” as a first argument. The second argument takes the destination path of the file, where we copy the original file.

Output:

The above snippet shows the destination path of the copied file named “sample_1”.

Method 3: Using “shutil.copy2()” Function

The “shutil.copy2()” function is similar to the “shutil.copy()” function, but this function preserves the metadata while copying the file. The below code shows the working of this method:

Code:

import shutil
shutil.copy2('sample.txt', 'C:/Users/p/Documents/ILf/sample_copy.txt')

In the above example code, the “shutil.copy2()” function copies the file by accepting the file name and destination path as an argument.

Output:

The above snippet shows the destination path of the copied file named “sample_copy”.

Method 4: Using os.rename() Function

The “os” module function “os.rename()” function is used to rename the files or directories in Python. The “os.rename()” function is utilized in the below code to copy a file:

Code:

import os
os.rename(r'C:\Users\p\Documents\program\sample.txt',r'C:\Users\p\Documents\ILF\sample_copy.txt')

In the above code, the “os.rename ()” function accepts the original file with the complete path as the first argument. The second argument takes the file’s complete path with the new name “sample_copy.txt”.

Output:

Method 5: Using “shutil.copyfileobj()” Function

The “shutil.copyfileobj()” function copies the file object from the source path to a targeted destination path. In the bellow code, the “shutil.copyfileobj()” function copies the file in the same directory: 

Code:

import shutil
orginal_file = open('sample.txt', 'rb')
targeted_file = open('sample_new.txt', 'wb')
shutil.copyfileobj(orginal_file, targeted_file)
targeted_file.close()

In the above code, the “open()” function is used with “rb” mode to read from the original file and “wb” mode to write to the copied file. The “shutil.copyfileobj()” copies the file from the source to the targeted destination.

Output:

The above snippet shows the path of the copied file named “sample_new”.

Conclusion

To copy a file, the “shutil.copyfile()”, “shutil.copy()”, “shutil.copy2()”, “os.rename()”, and “shutil.copyfileobj()” functions are used in Python. The “shutil.copyfile()” function copies the source file and places it at the targeted destination without copying its metadata. While the “shutil.copy()” function copies the file and permission mode to the destination path. This post delivered various guides on how to copy a file using Python.