pythonpython-3.xvisual-studio-codebinaryfiles

How do I modify and save the changes made to a file using Python and VSCode?


I'm trying to modify the contents of a SET file by copying some of the data that is different from another SET file. The SET files come from two different GPS receivers of the same model, and the files have the same name. The reason I'm doing this is because one of the GPS receivers is stuck in a boot loop, so I thought if I copy some of the data from the functioning GPS onto the malfunctioning one, such that they have identical data in their respective files, it might fix the problem.

I've tried accomplishing this using Python version 3.12 with VSCode, and although the code executes without any errors, there doesn't seem to be any changes made to the file I wanted to modify. This is the code I ran:

from itertools import zip_longest

count = 0

with open(r"C:\Users\jimal\Desktop\APP2\mgnShell.set", "r+b") as faulty_magellan, open(r"E:\APP\mgnShell.set", "rb") as magellan:
    faulty_mgln_binary = faulty_magellan.read()
    faulty_mgln_byte_array = bytearray(faulty_mgln_binary)
    magellan_binary = magellan.read()
    magellan_byte_array = bytearray(magellan_binary)
    for faulty_mgln_byte, magellan_byte in zip_longest(faulty_mgln_byte_array, magellan_byte_array):
        count +=1
        if faulty_mgln_byte != magellan_byte:
            faulty_magellan.seek(faulty_mgln_byte_array.index(faulty_mgln_byte))
            if magellan_byte == None:
                faulty_magellan.write(bytes(0x00))
            else:
                faulty_magellan.write(bytes(magellan_byte))
            
print(f"Done! Count was {count}")

I believe the r+b command in the open function should allow reading, writing and modifications to the file, but when I open the file after running the code no changes have been made. I am using Windows 11, and I noticed that the folder that contains the file, called APP2, has the read-only attribute selected. I've tried to unset that attribute but when I re-click the properties option of the APP2 folder the read only attribute has been set again. However, to test whether it's truly read only I made a text file in the APP2 folder and added some content, then modified its content, and the changes were saved successfully. I also tried to modify the file the SET file manually and those changes were made successfully. I've tried restarting VSCode as that has helped before but this time it didn't make any difference. Is the problem with the code I'm running or is there another cause for this issue?


Solution

  • By the grace of God I figured out the problem was in my code. First, I had to write the data to the file after the for loop, not in it. The reason is that all changes to the file have to be made before writing to the file in order for the changes to be successfully applied. Secondly, I have to make the changes to the byte array and then write the byte array to the file, rather than making changes directly to the file. This is what the code looked like after making the corrections:

    count = 0
    
    with open(r"C:\Users\jimal\Desktop\APP2\mgnShell.set", "rb+") as faulty_magellan, open(r"E:\APP\mgnShell.set", "rb") as magellan:
        faulty_mgln_binary = faulty_magellan.read()
        faulty_mgln_byte_array = bytearray(faulty_mgln_binary)
        magellan_binary = magellan.read()
        magellan_byte_array = bytearray(magellan_binary)
        for faulty_mgln_byte, magellan_byte, faulty_mgln_index in zip(faulty_mgln_byte_array, magellan_byte_array, range(len(faulty_mgln_byte_array))):
            count +=1
            if faulty_mgln_byte != magellan_byte:
                faulty_mgln_byte_array[faulty_mgln_index] = magellan_byte
        truncated_data = faulty_mgln_byte_array[0:len(magellan_byte_array)]
        faulty_magellan.seek(0)
        faulty_magellan.write(truncated_data)
        faulty_magellan.truncate()
                    
    print(f"Done! Count was {count}")