Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert large binary file to hexadecimal (binascii.hexlify returns MemoryError)

Tags:

python

I am trying to convert a large file (~1GB) into hexadecimal string with binascii (which works great on smaller files), but it causing a MemoryError.

this is the code I am using:

import binascii

filePath = "demo/11.mp4.zip"

file = open(filePath, "rb")
with file:
    byte = file.read()
    hexa = binascii.hexlify(byte)

hexa_string = hexa.decode('ascii');

any advice?

like image 713
Or Bachar Avatar asked May 05 '26 05:05

Or Bachar


2 Answers

Read your file in chucks:

import binascii

file_path = 'demo/11.mp4.zip'

chunk_size = 1024
with open(file_path, 'rb') as f:
    while True:
        data = f.read(chunk_size)
        if not data:
            break
        hexa = binascii.hexlify(data)
        hexa_string = hexa.decode('ascii')
        # work with hex string
like image 172
Delimitry Avatar answered May 11 '26 14:05

Delimitry


Actually You are approximately using 3 GB of your memory with storing the file and its hex within byte and hexa variables, instead of that you can iterate within your file and process the it based on chunks .

like image 35
Mazdak Avatar answered May 11 '26 15:05

Mazdak