Python write() not writing the full string

69 views Asked by At

So I made a script where you put hex data in for something like an image or an audio file inside a variable and it then copies it into its own file (self extracts). However it doesn't work with a file over 2MB.

I've tested it with files up to 2MB and it works fine but when the hex data size is above 2MB it doesn't write the full output to the file and you end up with a file that's a fraction of the original size. Here is my code (the example hex data is just random):

import os
import binascii

def save_image(filename, hex_imgdata):
    if os.path.exists(filename):
        current_file = open(filename, "wb")
    else:
        current_file = open(filename, "xb")
    decoded_imgdata = bytes.fromhex(hex_imgdata)
    current_file.write(decoded_imgdata)
    current_file.close()
    print("Saved " + filename)

example_hex = "A2C688B272"

if not os.path.exists("extracted"):
    os.mkdir("extracted")
os.chdir("extracted")

save_image(filename = "example.png", hex_imgdata = example_hex)

How can I make it work with larger files?

0

There are 0 answers