1 回答
TA贡献1893条经验 获得超10个赞
你不能,至少你的装饰器的编写方式是这样。当你装饰一个函数时,它类似于:
def execute_multi_commands(command, count):
LOG.info(f'Executing command {count}: {command}')
os.system(command)
count += 1
execute_multi_commands = read_commands(execute_multi_commands)
所以在这之后,read_commands已经被执行了,并且文件已经被读取了。
您可以做的是更改装饰器以读取包装器中的文件,例如:
def read_commands(inner, path=BATCH_PATH):
def wrapper(*args, **kwargs):
if "path" in kwargs:
path_ = kwargs.pop("path")
else:
path_ = path
with open(path_) as f:
commands = ['python ' + line.replace('\n', '') for line in f]
for command in commands:
inner(command, *args, **kwargs)
return wrapper
...但这意味着每次调用修饰函数时都会读取该文件,这与您之前所做的略有不同。
添加回答
举报