Skip to content
Related Articles

Related Articles

Improve Article

Python program to modify the content of a Binary File

  • Last Updated : 14 Aug, 2021

Given a binary file that contains some sentences (space separated words), let’s write a Python program to modify or alter any particular word of the sentence.

Step 1: Searching for the word in the binary file. 
Step 2: While searching in the file, the variable “pos” stores the position of file pointer record then traverse(continue) reading of the record. 
Step 3: If the word to be searched exists then place the write pointer (to ending of the previous record) i.e. at pos. 
Step 4: Call write() function to take the new record. 
Step 5: Write the new object at the position “pos” and hence the record is updated and print “record successfully updated”. 
Step 6: If the word does not exists then print “record not found”.

Let’s suppose the content of the binary file is:



# Python program to modify the
# content of binary file
# Function to update the
# content of binary file
def update_binary(word, new)
    # string variable to store
    # each word after reading
    # from the file
    string = b""
    # Flag variable to check
    # if the record is found or
    # not
    Flag = 0
    # Open the file in r + b mode which means
    # opening a binary file for reading and
    # writing
    with open('file.txt', 'r + b') as file:
        pos = 0
        # Reading the content of the
        # file character by character
        data = string =
        # Looping till the end of
        # file is reached
        while data:
            data =
            # Checking if the space is reached
            if data == b" ":
                # checking the word read with
                # the word entered by user
                if string == word:
                    # Mpving the file pointer
                    # at the end of the previously
                    # read record
                    # Updating the content of the file
                    Flag = 1
                    # storing the position of
                    # current file pointer i.e. at
                    # the end of previously read record
                    pos = file.tell()
                    data = string =
                # Storing the data of the file
                # in the string variable
                string += data
    if Flag:
        print("Record successfully updated")
        print("Record not found")
# Driver code
# Input the word to be found
# and the new word
word = input("Enter the word to be replaced: ").encode()
new = input("\nEnter the new word: ").encode()
update_binary(word, new)



Text file:



 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :