Append Data To Binary File In Python
Last Updated :
29 Feb, 2024
We are given a binary file and our task is to append data into that binary file in Python using different approaches. In this article, we will see how we can append data to binary file in Python.
Append Data to Binary File in Python
below, are the examples of Append Data To Binary File In Python:
- Appending String Datatype
- Append List of Integers
- Append Data from Another binary File
Appending String Datatype To Binary File In Python Programming
In this example, below code opens the binary file ‘binary_file.bin’ in append mode, allowing new data to be added without overwriting existing content. It defines binary data ‘data’ to be appended and writes it to the file.
Python3
with open ( 'binary_file.bin' , 'ab' ) as file :
data = b 'New data to append\n'
file .write(data)
print ( "Data has been appended to the binary file." )
|
Output
Data has been appended to the binary file.
Appending a String to the binary file
Append List of Integers To Binary File In Python Programming
In this example, below code appends a list of integers [1, 2, 3, 4, 5]
to the binary file ‘binary_file.bin’. Inside the loop, each integer is converted to a 4-byte big-endian representation using to_bytes
method, and then these byte sequences are written to the file.
Python3
data = [ 1 , 2 , 3 , 4 , 5 ]
with open ( 'binary_file.bin' , 'ab' ) as file :
for num in data:
file .write(num.to_bytes( 4 , byteorder = 'big' ))
|
Output
integers appended from a list to a binary file
Append Data from Another Binary File In Python Programming
Step 1: Create New File code
below code creates a new binary file ‘another_binary_file.bin’ and writes the sample data, which is a byte-encoded string containing the message “Hello, this is another binary file!”. The file is opened in binary write mode (‘wb’) to ensure proper handling of binary data.
Python3
with open ( 'another_binary_file.bin' , 'wb' ) as file :
data = b '\nHello, this is another binary file!\n'
file .write(data)
|
Output
New binary file created
Inside the new binary file
Step 2: Append Data to Binary File
Below code reads the contents of the binary file ‘another_binary_file.bin‘ into the variable `binary_data` using binary read mode (‘rb‘). It then appends this binary data to the original binary file ‘binary_file.bin‘ in append mode (‘ab’). This allows the data from one binary file to be saved and appended to another.
Python3
with open ( 'another_binary_file.bin' , 'rb' ) as source_file:
binary_data = source_file.read()
with open ( 'binary_file.bin' , 'ab' ) as destination_file:
destination_file.write(binary_data)
|
Output
Data added from the new file to the original file
Conclusion
In conclusion , Appending data to a binary file in Python is a useful operation when working with non-textual data or complex data structures. The ability to efficiently add new data while preserving the existing structure makes binary files a preferred choice for various applications. Understanding the syntax and advantages of appending data to binary files empowers developers to handle diverse data storage scenarios in their Python projects.
Share your thoughts in the comments
Please Login to comment...