【发布时间】:2022-02-05 18:12:59
【问题描述】:
我想从 txt 文件中执行 python 代码并存储它。
在 txt 文件中我有这个代码:
def func(a):
return a
在python中:
def random_file_input(file_name):
ran_number = random.randint(0, 100)
with open(file_name, 'r+') as f:
data = f.read()
f.write(f"\nfunc({ran_number})")
a = exec(data) #-----> i want to store like this :(
random_file_input('python_code.txt') ```
【问题讨论】:
-
已经存在类似问题:(stackoverflow.com/questions/31704916/…)
标签: python function file exec python-exec