• 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.
  • Posts

    • Minor update.  All is going well for the most part.  Picked up some new information today though from my server logs. Just discovered a new #ByteDance scraper's useragent; imageSpider.  More specifically: "Mozilla/5.0 (compatible; imageSpider; scrapedia-receive@bytedance.com)" Added to my list. I'm also adding #Alibaba IP ranges to a "drop" rule on my firewall because they've aggressively scraping me (hundreds of thousands of requests per day), but they're not using accurate user agents.  They're ignoring robots.txt (big surprise) and pretending to be everything from Chrome 114 to Internet Explorer 6 and not self identifying as a bot.
    • TLDR: Doom 2016 style reboot, which is what we have been asking since Bungie announced this "reboot"
    • I'm gonna take the bait but here we go: It's closed source as far as Linux goes but it is not locked to the Steam as a gaming platform. Yes, you need a Steam account to start using SteamOS, but after that you can use whatever Linux compatible launcher you want, ie Heroic or Lutris It doesn't need an antivirus, it's Arch Linux. Anticheats work, depending on the game and publisher. Many games with EAC work. None with kernel level malware anticheat work, and they should NOT work on Windows either (MS really needs to block this). It is compatible with Linux applications, Flatpak applications. You can also enable regular Pacman/AUR yourself. It's disabled, not removed, out of the box to protect the OS from you But surely you do know you don't have to use SteamOS on the Steam Deck, right?
    • Preparation of bringing subscription model to WhatsApp. Pathetic and greedy.
    • Doing that has only worked for me with the Enterprise version.
  • Recent Achievements

    • Week One Done
      Peterlll06 earned a badge
      Week One Done
    • One Month Later
      gatilogistic earned a badge
      One Month Later
    • One Year In
      Custom Greek Shirts earned a badge
      One Year In
    • One Month Later
      Custom Greek Shirts earned a badge
      One Month Later
    • Week One Done
      Custom Greek Shirts earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      673
    2. 2
      ATLien_0
      281
    3. 3
      Michael Scrip
      222
    4. 4
      +FloatingFatMan
      190
    5. 5
      Steven P.
      145
  • Tell a friend

    Love Neowin? Tell a friend!