Looking for some help with script

New to the forum, I hope this is in the correct place. Its purpose is to search a binary file, pages (4096) that are full of EE and write them to FF like they should be(see comments), so far this code never opens my file and runs**. I know it has an unused import I have changed this script alot and tried some different tools within it.**

import mmap
import os
import sys
filename = “2101.bin”

def replace_ee_pages_with_ff(filename, page_size=4096):
    “”"
    Detects pages filled with 0xEE bytes in a binary file and replaces them
    with pages of 0xFF bytes.

    Args:
        filename (str): The path to the binary file.
        page_size (int): The size of a memory page in bytes (default is 4096).
    """
    # Define the byte patterns
    pattern_EE = b'\xEE' * page_size
    pattern_FF = b'\xFF' * page_size

    try:
        # Open the file in binary read/write mode
        with open(filename, 'r+b') as f:
            # Memory map the file, allowing in-place modification
            with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE) as mm:
                file_size = len(mm)
                print(f"File size: {file_size} bytes. Page size: {page_size} bytes.")

                # Iterate through the file in page-sized chunks
                for offset in range(0, file_size, page_size):
                    # Check if the current chunk is a full page
                    if offset + page_size <= file_size:
                        # Read the page content
                        current_page = mm[offset:offset + page_size]

                        # Check if the page is entirely 0xEE
                        if current_page == pattern_EE:
                            # If it is, replace it with 0xFF bytes
                            mm[offset:offset + page_size] = pattern_FF
                            print(f"Replaced page at offset {offset:#x}")

                # Force the changes to be written to disk
                mm.flush()
                print("Replacement complete. Changes flushed to disk.")

    except FileNotFoundError:
        print(f"Error: The file '{filename}' was not found.")
        sys.exit(1)
    except Exception as e:
        print(f"An error occurred: {e}")
        sys.exit(1)


any help would be great thank you!

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

OK got it thank you.

…………………………………………nobody?

Please don’t bump a thread like this.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too.

However, as you’ve said

This isn’t much to go on. It’s hard to debug code that is meant to run on your system.

Maybe you can provide more context.

How can you tell the file isn’t opened?

any error messages? What happens?

EDIT: Do you have an example 2101.bin file to test?

You have a function here, but I don’t see a line actually calling the function?