Jasmine
Jasmine

Reputation: 476

Error while writing an array of bytes to a File in python

Input Byte array = [b'\x03\x00', b'\x04\x00', b'\x05\x00', b'\x05\x00', b'\x05\x00', b'\x06\x00', b'\x07\x00', 
b'\x08\x00', b'\t\x00', b'\n\x00', b'\t\x00', b'\x08\x00', b'\x07\x00', b'\x06\x00']

I need to write the above array to a binary file and to verify if the data is written correctly I am reading back the same file. But While reading back it is giving me an empty list. Can someone point out the error here??

#Values array is calculated from the previous steps
intermediate = [int.from_bytes(x, byteorder='big', signed=True) for x in values]
print("Integer in Big Endian {}".format(intermediate))

fileData = [byte.to_bytes(2, byteorder='little', signed=True) for byte in intermediate]
print("Data written to the Binary file{}".format(fileData))
#I am printing the array of bytes that I am writing to the file.


newFile = open('./flash.dat', "wb")
for byte in intermediate:
    newFile.write(byte.to_bytes(2, byteorder='little', signed=True))


file = open("./flash.dat", "rb")
number=file.read()
file.close()
print("Data in the binary file {}".format(number))

The result is this

 Integer in Big Endian [3, 4, 5, 5, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6]
 Data written to the Binary file[b'\x03\x00', b'\x04\x00', b'\x05\x00', b'\x05\x00', b'\x05\x00', b'\x06\x00', 
 b'\x07\x00', b'\x08\x00', b'\t\x00', b'\n\x00', b'\t\x00', b'\x08\x00', b'\x07\x00', b'\x06\x00']
 Data in the binary file b''

Upvotes: 1

Views: 749

Answers (2)

Lucas
Lucas

Reputation: 7331

You are not flushing the file (the changes remain in memory, they are never saved to disk).

newFile = open('./flash.dat', "wb")
for byte in intermediate:
    newFile.write(byte.to_bytes(2, byteorder='little', signed=True))
# save to disk
newFile.flush()
# or close the file (this also flush the change to disk)
newFile.close()

Or better, with a context manager:

# If we use the with (context manager) it automatically flushes and closes the file
with open('./flash.dat', "wb") as new_file:
    for byte in intermediate:
        new_file.write(byte.to_bytes(2, byteorder='little', signed=True))

# the same for reading the file
with open("./flash.dat", "rb") as file:
    number=file.read()

print("Data in the binary file {}".format(number))
# Data in the binary file b'\x00\x03\x00\x04\x00\x05\x00\x05\x00\x05\x00\x06\x00\x07\x00\x08\x00\t\x00\n\x00\t\x00\x08\x00\x07\x00\x06'

Also: you shouldn't use camelCase. In python is never used (look pep08).

Instead of newFile use new_file, fileData use file_data

Upvotes: 2

RajeshM
RajeshM

Reputation: 872

You need to close the file after writing to it before opening it again. Due to buffering, the file may not be written to disk until it is closed or the buffer is flushed. Closing the file flushes the buffer and writes it to disk.

newFile = open('./flash.dat', "wb")
for byte in intermediate:
    newFile.write(byte.to_bytes(2, byteorder='little', signed=True))
newFile.close()

Upvotes: 1

Related Questions