• 0

[Python] Removing Bytes at Particular Offsets in a File


Question

Howdy Folks,

I've done a fair share of Googling but to no avail so I thought I'd get some input on my current task here. I've got a file of raw packet data that I need to do some manipulation to. Basically, I need to delete the first several bytes of data in the file, then delete another set amount of bytes occurring at a particular offset, then another set of bytes at another offset, and so on and so forth.

The basic logic is this:

Delete first 1048 bytes of the file

Skip to offset 0x10000

Delete 68 bytes of data

Skip to offset 0x20000

Delete 68 bytes of data

Skip to offset 0x30000

Delete 68 bytes of data

I'd appreciate any thoughts or general pointers on how I should approach this in python. I've got a few other things I eventually want to do with this script but getting this figured out should point me in the right direction.

2 answers to this question

Recommended Posts

  • 0

I'm not sure how you could delete bytes in a file directly. What you could do is write the parts you want to keep in a temporary file, then delete the original and rename the temporary file to the original name.

The file object in python has all the methods you need to do this: open, read, seek, write. Oh and you'll need os.remove() to delete the temporary file.

  • 0
  On 21/04/2011 at 17:17, Dr_Asik said:

I'm not sure how you could delete bytes in a file directly. What you could do is write the parts you want to keep in a temporary file, then delete the original and rename the temporary file to the original name.

The file object in python has all the methods you need to do this: open, read, seek, write. Oh and you'll need os.remove() to delete the temporary file.

Probably even better would be using mmap this would negate the need for temporary file and deleting.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.