Skip to content

Instantly share code, notes, and snippets.

@geekman
Last active July 12, 2023 17:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekman/8144299d8a8788b1c76370a619bedd1a to your computer and use it in GitHub Desktop.
Save geekman/8144299d8a8788b1c76370a619bedd1a to your computer and use it in GitHub Desktop.
removes a block within a file and shifts its following contents "upwards"
# moves up file data at current pos to specified to_offset
def moveup(f, to_offset, block_size=4096):
assert f.tell() > to_offset
while True:
data = f.read(block_size)
newpos = f.tell()
f.seek(to_offset)
to_offset += len(data)
if len(data) == 0: break
f.write(data)
f.seek(newpos)
f.truncate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment