Copying a file is important in Python when you want to make a file backup before modifying it utilizing your script, or when you want to move and rename a file to another location. Python provides various modules and functions to copy a file in Python including the “shutil” module.
This Python tutorial provides various ways to copy a file in Python. Some of the methods are listed below:
- “shutil.copyfile()” Method.
- “shutil.copy()” Method.
- “read()” and “write()” Functions.
- “shutil.copyfileobj()” Method.
Method 1: Copy a File in Python Using “shutil.copyfile()” Method
In Python, the “shutil.copyfile()” method copies the contents (excluding metadata) of the original file to the target file and returns the destination/target file.
Syntax
In the above syntax, “src” is the original/source file path, and “dst” refers to the destination/final file path. The “follow_symlinks” parameter is optional and its default value is “True”. It will create a symbolic link instead of copying the file if it is set to “False“.
The below snippet shows the original file location along with its name:
Example
Here is an example code:
source_file = r'C:\Users\p\Documents\program\filename.txt'
destin_file = r'C:\Users\p\Downloads\Software\filename.txt'
shutil.copyfile(source_file, destin_file)
In the above code, the “shutil.copyfile()” method copies the file by taking the original/source file and destination file locations as their arguments, respectively.
Output
The above snippet verified that the specific file has been copied from one location to another.
Method 2: Copy a File in Python Using “shutil.copy()” Method
The “shutil.copy()” method is utilized to copy the data of the original file to the target file. However, the metadata of the file is not copied.
Example
The below code is used to copy the file in Python:
source_file = r'C:\Users\p\Documents\program\filename.txt'
destin_file = r'C:\Users\p\Downloads\Software\filename.txt'
shutil.copy(source_file, destin_file)
In the above code lines, the “shutil.copy()” method takes the source/src and destination/dst file with complete locations as their arguments and copies the file accordingly.
Output
According to the above snippet, the specific file has been copied from one location to another appropriately.
Method 3: Copy a File in Python Using the “read()” and “write()” Functions
The “read()” and “write()” functions are used along with the “open()” function in read and write mode to copy the file from one location to another in Python.
Example
The below-stated code is utilized to transfer files between different paths:
with open(r'C:\Users\p\Downloads\Software\filename.txt', 'w') as df:
df.write(sf.read())
In the above code:
- Firstly, we open a source file named “txt” using the “open()” function.
- After that, another file “txt” is created using the “open()” function in the destination folder.
- The “read()” function reads everything from the given source file and writes it to the destination file using the “write()” function.
Output
This snippet shows that the file was successfully copied.
Method 4: Copy a File in Python Using the “shutil.copyfileobj()” Method
The “shutil.copyfileobj()” method is utilized to copy the information of a file-like object to a different file-like object. By default, this method copies data in “64 KB” chunks, but you can specify a different buffer size using the “bufsize” argument.
Syntax
In the above syntax, “fsrc” is a file-like object representing the source file to be copied, and “fdst” is a file-like object representing the destination file where “fsrc” will be copied. The “optional” parameter length denotes buffer size.
Example
The below code is utilized to copy the file in Python:
source_file = open(r'C:\Users\p\Documents\program\filename.txt', 'rb')
destin_file = open(r'C:\Users\p\Downloads\Software\filename.txt', 'wb')
shutil.copyfileobj(source_file, destin_file)
In this code, the “shutil.copyfileobj()” method is utilized to copy the file from the source/original file to the destination file.
Output
The above snippet verified that the particular text file has been copied from source to destination.
Conclusion
To copy a file various methods are used in Python such as “shutil.copyfile()” method, “shutil.copy()” method, “read()” and “write()” functions or the “shutil.copyfileobj()” method. Most of these approaches utilize the “shutil” module to copy the file from the source/original path to the destination path by taking the file with the complete path as an argument. This guide presented various methods to copy the file in Python utilizing relevant examples.