I want to keep a piece of code in a txt file and execute it from my Python script.
For example in the txt file there is
print(\"ok\")
I
You are looking for eval
function
eval
is a function that takes arbitrary python code represented as a string and execute it at run time.
Example
>>> x = 1
>>> y = eval('x+1')
>>> print(y)
2
It works in both Python 2.x and 3.x
Check the documentation : https://docs.python.org/2.7/library/functions.html#eval