How can I utilize “executive” in Python to execute scripts in the main program?
I have developed a Python program that generates scripts based on inputs from the users. How can I utilize the “executive” keyword in Python programming language for executing the dynamic scripts that are generated within my main programming?
The “ python execfile” function generally is not part of Python, however, it is included in Python 2. This particular function is mainly used for executing the dynamic script based on user input. Here is the example given to showcase you:-
# Assuming Python 2. x environment
Def execute_dynamic_script(filename):
Try:
With open(filename, ‘r’) as script_file:
Script_code = script_file.read()
Exec script_code in globals(), locals()
Except IOError:
Print(“File not found or unable to open.”)
# Usage example:
User_input_filename = raw_input(“Enter the filename of the script to execute: “)
Execute_dynamic_script(user_input_filename)