[Solved] How to execute a function inside a python script on a string [closed]


First, it is not a good idea to do so from security viewpoint.

But, if you want to do it, you can do it this way:

import os

script = """
print("Executed!")
"""

with open('script.py','w') as fhand:
    fhand.write(script)

os.system('python script.py')

You can write the string in script to another *.py file and then run that script.

Edit:
If you want to execute the function, you could do somthing like this:

import os

scriipt = """
def hello():
    print('Executed')
"""

with open('script.py','w') as fhand:
    fhand.write(scriipt)

import script  //which is the name of the python file

script.hello()

One downside, you should know the name of the function.

2

solved How to execute a function inside a python script on a string [closed]