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
...但這意味著每次調用修飾函數時都會讀取該文件,這與您之前所做的略有不同。
添加回答
舉報