【发布时间】:2023-03-28 16:27:01
【问题描述】:
我正在尝试通过 Windows 终端停止执行脚本 (run_all.py),该终端运行列表中包含的多个文件,具体取决于每个文件中满足的特定条件。具体来说,在 file_2.py 中,我想根据“y”参数的值暂停该文件的执行,然后停止执行文件列表中尚未运行的任何文件(即 file_3.py)。我目前正在使用 time 模块将 file_2.py 暂停 60 秒,但我需要一种更优雅的方式来停止执行,而无需手动键盘命令(例如 Ctrl + C)......非常感谢任何帮助!
file_1.py
print("Math is fun!")
file_2.py
import os
import time
x=1
y=2
if x < 10:
print("x is small")
else:
print("x is invalid")
if y < 10:
print("y is also small")
else:
print('press "ctrl + c" to stop execution of the workflow')
time.sleep(60)
file_3.py
print("python is so cool!")
run_all.py(从 Windows 终端运行的主脚本)
import os
path = "C:\\users\\mdl518\\Desktop\\"
file_list = ['file_1.py', 'file_2.py', 'file_3.py']
for filename in file_list:
os.system('python' + ' ' + os.path.join(path,filename)) # runs all files in the file_list
【问题讨论】:
标签: python windows if-statement automation operating-system