Skip to content

Instantly share code, notes, and snippets.

@ILPlais
Last active December 25, 2024 23:12
Show Gist options
  • Select an option

  • Save ILPlais/bdef97efea0eb1a0b1ab879e48d9cf40 to your computer and use it in GitHub Desktop.

Select an option

Save ILPlais/bdef97efea0eb1a0b1ab879e48d9cf40 to your computer and use it in GitHub Desktop.
Remove useless bytes from a file
#!/bin/python3
import os
import argparse
from pathlib import Path
def remove_useless_bytes(file_path, file_destination_path, num_bytes_to_remove):
# Check if the file exists
if not file_path.exists():
print(f"File {file_path} does not exist")
return
# Read the file
with open(file_path, 'rb') as file:
data = file.read()
# Remove the beginning number of bytes
data = data[num_bytes_to_remove:]
# Write the file
with open(file_destination_path, 'wb') as file:
file.write(data)
if __name__ == "__main__":
# Parse the arguments
parser = argparse.ArgumentParser(description = "Remove useless bytes from a file")
parser.add_argument("-f", "--file_path",
type = Path,
required = True,
help = "Path to the file to process")
parser.add_argument("-d", "--file_destination_path",
type = Path,
required = True,
help = "Path to the file destination")
parser.add_argument("-n", "--num_bytes_to_remove",
type = int,
required = True,
help = "Number of bytes to remove")
args = parser.parse_args()
# Remove the useless bytes
remove_useless_bytes(args.file_path, args.file_destination_path, args.num_bytes_to_remove)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment