Reputation: 11
I need to prepend a part of binary to start of file(Prepend a binary). I try this:
s=b'/00/'
open(file,'rb') as o
r=o.read()
o.close()
h=r.hex()
w=s+h
b=bytearray.fromhex(w)
open(file,'wb')as o
o.write(b)
o.close()
I have memory limit and it is not possible to use that algorithm. Also I have to be able to remove this part from file, I used:
open(file,"br+") as o
r=o.read()
o.close()
h=r.hex()
s=h.replace('/00/','')
o=open(file,"wb")
w=bytearray.fromhex(s)
o.write(w)
o.close()
I have memory limit for this. Can Anyone help me to read and write to these files without load them completely in memory?
Upvotes: 0
Views: 78
Reputation: 106658
There's no way to prepend bytes into a binary without rewriting the entire file unless you want to deal with platform-dependent low-level filesystem manipulations.
Instead, you can read and rewrite the file in small chunks:
s=b'/00/'
with open("oldfile", "rb") as old, open("newfile", "wb") as new:
new.write(s)
for chunk in iter(lambda: old.read(1024), b""):
new.write(chunk)
Adjust the 1024-byte chunk size to one suitable to your memory constraints.
Upvotes: 1