Sunday, November 17, 2024
Google search engine
HomeLanguagesPython program to modify the content of a Binary File

Python program to modify the content of a Binary File

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.

Approach:
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”.

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

python-binary-file-input

Python3




# 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 = file.read(1)
 
        # Looping till the end of
        # file is reached
        while data:
            data = file.read(1)
 
            # Checking if the space is reached
            if data == b" ":
 
                # checking the word read with
                # the word entered by user
                if string == word:
 
                    # Moving the file pointer
                    # at the end of the previously
                    # read record
                    file.seek(pos)
 
                    # Updating the content of the file
                    file.write(new)
                    Flag = 1
                    break
                else:
                    # storing the position of
                    # current file pointer i.e. at
                    # the end of previously read record
                    pos = file.tell()
                    data = string = file.read(1)
            else:
 
                # Storing the data of the file
                # in the string variable
                string += data
                continue
 
 
    if Flag:
        print("Record successfully updated")
    else:
        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)


Output:

python-binary-file-output-1

Text file:

python-binary-file-output-2

 

RELATED ARTICLES

Most Popular

Recent Comments