Friday, May 13, 2022

[FIXED] How to append null (00 00 ect bytes) to a file until certain size via Python

Issue

I have a file that I need to append NULL or 00 ~ ect bytes to the end of the file (in hex) so the file size would be 625423968.

Right now the file size is: 606256432

I tried:

with open(f, 'wb') as binfile:
    binfile.write(b'\x00' - 19167536)

but my file size becomes 0

Doing it manually in Hex Editor takes way too long

Greatly appreciate all the help!


Solution

You can seek your file and then write the last NULL.

with open(f, 'wb') as binfile:
    binfile.seek(625423968 - 1)
    binfile.write(b'\x00')

(Maybe you have to use file-mode "br+ when you write your file not from scratch in python. This will keep the content and append the NULLs.)



Answered By - Sven Eberth
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.