
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy Binary File in Python
In Python, while working with files, we may need to duplicate binary files. In this article, we will go through various ways to explore efficient methods of copying binary files in Python. First, we need to understand about Binary format and Binary File Copying, before proceeding with various ways to copy a binary file.
What is Binary Format?
Binary format is a method of storing data in the form of binary digits, such as 0s and 1s. Unlike text format, which represents data using readable characters like letters and numbers, binary format stores data as raw byte sequences.
Binary file copying refers to the process of duplicating a file exactly as it is, and byte by byte, without interpreting or modifying its content. This is essential for copying files that contain non-text data such as images(.jpg, .png), Audio/Video(.mp3,.mp4), executables(.exe), PDFs, Word documents, and other binary formats. Copying the Binary File means handling the raw bytes of the file, not characters.
The following are the various methods available in Python to copy a Binary file -
Using shutil.copy()
The shutil.copy() function is one of the functions in the shutil Python built-in module, which is used to copy the contents of a file from a source to a destination while also copying basic file permissions. The following is the syntax of shutil.copy() function -
shutil.copy(src, dst)
Where,
- src: Path to the source file, which must be a file, not a directory.
- dst: Path to the destination file or directory. If dst is a directory, then the file is copied into it with the same name.
Example
The following is an example of using the function shutil.copy(), which is used to copy the binary file in Python -
import shutil source = r"D:\Tutorialspoint\Articles\sample.txt" # add your appropriate path destination = r"D:\Tutorialspoint\Backup\sample_copy.txt" try: shutil.copy(source, destination) print("File copied successfully.") except FileNotFoundError: print("Source file not found.") except PermissionError: print("Permission denied.")
Following is the output of the above code, which copies the binary file -
File copied successfully.
Using shutil.copyfile()
shutil.copyfile() is another function in the Python built-in shutil module. This is used to copy the contents of a file from a source to a destination. When we compare shutil.copyfile() with shutil.copy() it does not copy file metadata such as permissions. It performs a direct byte-for-byte copy of the file content.
The following is the syntax of the shutil.copyfile() function -
shutil.copyfile(src, dst)
Where,
- src: Path to the source file. Must be a file that is not a directory.
- dst: Path to the destination file. Must also be a file path, not a directory.
Example
The following is an example of using the function shutil.copyfile() to copy a binary file in Python -
import shutil source = r"D:\Tutorialspoint\Articles\sample.txt" # add your appropriate path destination = r"D:\Tutorialspoint\Backup\sample_copy.txt" try: shutil.copyfile(source, destination) print("File copied successfully.") except FileNotFoundError: print("Source file not found.") except PermissionError: print("Permission denied.")
Here is the output of the above code, which copies the binary file -
File copied successfully.
Using with open() in Binary Mode
Another method in Python is the open() function, which is used to copy binary files manually. By opening the source file in read-binary ('rb') mode, then the destination write-binary ('wb') mode will read the contents as raw bytes and write them directly by ensuring all data is preserved without any transformation.
The following is the syntax used to copy binary files using the open() function -
with open(src, 'rb') as source_file, open(dst, 'wb') as destination_file: destination_file.write(source_file.read())
Where,
- src: Path to the source binary file such as image, video or executable.
- dst: Path to the destination file where the binary data is copied.
- 'rb': Opens the file for reading in binary mode.
- 'wb': Opens the file for writing in binary mode.
Example
Following is an example of using the open() function to copy a binary file in Python -
source = r"D:\Tutorialspoint\Articles\sample.txt" # Add your appropriate path destination = r"D:\Tutorialspoint\Backup\sample_copy.txt" try: with open(source, 'rb') as src_file, open(destination, 'wb') as dst_file: dst_file.write(src_file.read()) print("File copied successfully.") except FileNotFoundError: print("Source file not found.") except PermissionError: print("Permission denied.")
Here is the output of the above code, which copies the binary file -
File copied successfully.
Using shutil.copy2()
shutil.copy2() is one more function in Python's built-in shutil module, which is used to copy the contents of a file from a source to a destination. In addition to copying the file data and permissions like shutil.copy(), it also attempts to preserve the original file's metadata such as timestamps to be created, modified, and accessed times.
Following is the syntax of the shutil.copy2() function -
shutil.copy2(src, dst)
Where,
- src: Path to the source file, which must be a file.
- dst: Path to the destination file or directory. If dst is a directory then the file is copied into it with the same name.
Example
Following is the example of using the function shutil.copy2(), which is used to copy the binary file in Pytho,n along with preserving metadata -
import shutil source = r"D:\Tutorialspoint\Articles\sample.txt" # Add your appropriate path destination = r"D:\Tutorialspoint\Backup\sample_copy.txt" try: shutil.copy2(source, destination) print("File copied successfully with metadata.") except FileNotFoundError: print("Source file not found.") except PermissionError: print("Permission denied.")
Following is the output of the above code which copies the binary file and preserves metadata -
File copied successfully with metadata.