Append Data to Binary File in Python
below, are the examples of Append Data To Binary File In Python:
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
# Opening the created binary file in append mode with open ( 'binary_file.bin' , 'ab' ) as file : # Data to append data = b 'New data to append\n' # Writing the data to the file file .write(data) print ( "Data has been appended to the binary file." ) |
Output
Data has been appended 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 ] # Opening the same binary file in append mode with open ( 'binary_file.bin' , 'ab' ) as file : # Converting integers to bytes and append to the file for num in data: file .write(num.to_bytes( 4 , byteorder = 'big' )) |
Output
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
# Creating another binary file with open ( 'another_binary_file.bin' , 'wb' ) as file : # Sample data data = b '\nHello, this is another binary file!\n' # Writing the data to the file file .write(data) |
Output
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
# Saving the data from one binary file with open ( 'another_binary_file.bin' , 'rb' ) as source_file: binary_data = source_file.read() # Writing the data of the previous file in the original file with open ( 'binary_file.bin' , 'ab' ) as destination_file: destination_file.write(binary_data) |
Output
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.
Append Data To Binary File In Python
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.