import os program = 'program' with open(program, "w") as fp: print("#!/usr/bin/env bash", file=fp) print('echo "execv() searchs in the current directory"', file=fp) fp.flush() os.chmod(program, 0o777) args = [program] os.execv(args[0], args)