我有一个 python 脚本 script_a.py
,它使用 subprocess.call()
执行另一个脚本 script_b.py
作为最后一条指令在结束之前。我需要 script_b.py
等待 script_a.py
关闭,然后再继续执行其自己的指令。为此,我在 script_b.py 中使用 while 循环。我怎样才能做到这一点?我当前尝试过的所有解决方案都让 script_a.py
等待 script_b.py
完成后再自行关闭。我有一种感觉,这可能涉及 atexit()
或类似的东西,但我迷路了。
非常感谢!
最佳答案
你可以做一些完全黑客的废话
script_b.py
while not os.path.exists("a.done"):pass
time.sleep(0.2) # a little longer just to be really sure ...
os.remove("a.done")
... # rest of script b
script_a.py
import atexit
atexit.register(lambda *a:open("a.done","w"))
或者代替 Popen
只是这样做
os.execl("/usr/bin/python","script_b.py")
关于python - 执行另一个python脚本,然后关闭当前脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38509674/