有个exe程序需要等另一个程序结束之后执行,
所以需要判断某个exe程序是否关闭,若关闭继续执行,否则循环判断
ChatGPT尝试为您解答,仅供参考
在 Python 中,你可以使用 psutil 库来判断某个 exe 程序是否关闭。
首先,你需要安装 psutil 库:
pip install psutil
然后,你可以使用以下代码来判断某个 exe 程序是否关闭:
import psutil
def is_process_running(process_name):
"""Check if a process with the given name is running."""
for process in psutil.process_iter():
try:
if process_name.lower() in process.name().lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
# 检查 "example.exe" 是否正在运行
if is_process_running("example.exe"):
print("example.exe is running")
else:
print("example.exe is not running")
你可以在循环中使用这段代码来持续检查 exe 程序是否关闭,例如:
while is_process_running("example.exe"):
print("example.exe is still running, waiting...")
time.sleep(1)
print("example.exe has closed, continuing execution")
注意:这段代码只能在 Windows 系统上使用,如果你的程序运行在其他系统上,你需要使用不同的方法来判断进程是否存在。
提供参考实例【Python 脚本监控.py脚本的进程状态,实现中断重启】,实例简单易懂,链接:https://mr96s.com/1893.html
你可以使用 Python 的 psutil 库来判断某个进程是否还在运行。
首先,需要安装 psutil 库:
pip install psutil
然后,可以使用以下代码来判断某个进程是否还在运行:
import psutil
def is_process_running(process_name):
for proc in psutil.process_iter():
try:
if proc.name() == process_name:
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
# 示例:判断 notepad.exe 是否还在运行
if is_process_running("notepad.exe"):
print("notepad.exe is still running")
else:
print("notepad.exe is not running")
也可以使用以下代码来循环判断,直到指定的进程关闭为止:
import time
while is_process_running("notepad.exe"):
print("notepad.exe is still running, waiting...")
time.sleep(1)
print("notepad.exe has closed, continuing execution...")
python 判断某个exe是否关闭
借鉴下
http://t.zoukankan.com/moshuixiong-p-12183026.html