hi. i have a really basic question (really new to python). how do i read an entire binary file? from https://www.devdungeon.com/content/working-binary-data-python , data = binary_file.read() is to read the whole file at once. so i have a .dng file to test, which is 16.2mb, and i did:

with open("portrait1.dng", "rb") as binary_file:
        #Read the whole file at once
        data = binary_file.read()
        #Return the hexadecimal representation of the binary data, byte instance
        hexa = binascii.hexlify(data)
        with open("readprotrait1.raw", "wb") as write_file:
              write_file.write(hexa)

im using a hex editor to see if the data from .dng gets copied to the .raw file but only a portion of it gets copied. same result goes when i use a while loop like :

while True:
      data = binary_file.read()
      if not data:
           break
      #return hex rep and write

if i put a size(1024) in the .read(), only the last few lines of the data is written. so does it actually read the whole file and at the "write" part is where it gets complicated(doesn't write complete)? or am i missing something?

Okay. I just found bytearray() which has accomplished my goals by:

with open("portrait1.dng", "rb") as binaryfile :
    myArr = bytearray(binaryfile.read())

with open("readfile.raw", "wb") as newFile:
    newFile.write(myArr)

but my questions still sort of stand because as i mentioned the current file i am testing is only 16.2mb and the real file which will be used is 8gb and i tested this code with that huge file and my laptop froze. so a couple of updated questions/thoughts:

  1. read and write obviously works but which operation uses most RAM? as one of it should be the reason why my laptop froze. is there a way to lessen that load? coz i can't keep testing and having my laptop freeze, that is surely unhealthy for it. my whole university life is on this (translation, if it dies i die).

  2. is there a better way to read the file? like portion by portion perhaps? how is that achieved? because i dont really need the write, the write is just for me to see that the file is wholly read. i need the read for other processes.

Read the file by chunks and also write chunks:

with open("portrait1.dng", "rb") as binaryfile :
    with open("readfile.raw", "wb") as newFile:
        while True:
            chunk = binaryfile.read(4096)
            if not chunk:
                break
            newFile.write(binascii.hexlify(chunk))

my whole university life is on this (translation, if it dies i die).

Make sure you backup your files regularly on a separate disk.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.