gpt4 book ai didi

python - 如何从另一个 python 脚本运行和停止 python 脚本?

转载 作者:行者123 更新时间:2023-12-02 06:59:04 25 4
gpt4 key购买 nike

我想要这样的代码:

if True:
run('ABC.PY')
else:
if ScriptRunning('ABC.PY):
stop('ABC.PY')
run('ABC.PY'):

基本上,我想运行一个文件,比如说abc.py,并且基于一些条件。我想停止它,然后从另一个 python 脚本再次运行它。可能吗?

我使用的是 Windows。

最佳答案

您可以使用 python Popen用于在子进程中运行进程的对象

因此 run('ABC.PY') 将是 p = Popen("python 'ABC.PY'")

if ScriptRunning('ABC.PY) 将是 if p.poll() == None

stop('ABC.PY') 将是 p.kill()

关于python - 如何从另一个 python 脚本运行和停止 python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58570742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com