Python 运行else循环一次
所以我有一个带有代码的脚本Python 运行else循环一次,python,loops,psutil,Python,Loops,Psutil,所以我有一个带有代码的脚本 import os import subprocess import psutil def checkIfProcessRunning(processName): ''' Check if there is any running process that contains the given name processName. ''' #Iterate over the all the running process for proc in psutil.proc
import os
import subprocess
import psutil
def checkIfProcessRunning(processName):
'''
Check if there is any running process that contains the given name processName.
'''
#Iterate over the all the running process
for proc in psutil.process_iter():
try:
# Check if process name contains the given name string.
if processName.lower() in proc.name().lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False;
然后在执行之后
while True:
if checkIfProcessRunning('TDR'):
print('TDR (tdr.exe) is running')
else:
print('TDR (tdr.exe) is not running')
subprocess.call("cmd /c data.vbs") # Executes other code
整个脚本检测进程tdr.exe是否打开,当代码检测到它未打开时,我希望它打开其他一些代码,但我希望它只打开一次,而不是循环。请帮助我理解,这里的问题是,在调用其他代码a.k.a data.vbs后,是否继续执行它?在subprocess.callcmd/c data.vbs之后添加一个中断。 或者像这样构造它:
while True:
if not checkIfProcessRunning('TDR'):
break
print('TDR (tdr.exe) is running')
# Once we find TDR is dead, we execute our other code
print('TDR (tdr.exe) is not running')
subprocess.call("cmd /c data.vbs") # Executes other code
我希望这有帮助:D请修复代码的缩进我尝试了你的技巧并替换了代码,但在运行后我得到了错误类型error:checkIfProcessRunning缺少1个必需的位置参数:“processName”似乎你调用了checkIfProcessRunning,但没有传递任何参数。