Can you create a function that has custom code via parameters in python?

pass the function itself:

def write_to_file(func):
    with open(path, 'r+', encoding="utf-8") as file:
        content = file.read()
        file.seek(0) 
        func()
        file.truncate()

def func():
    print("foo")

write_to_file(func)