Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行else循环一次_Python_Loops_Psutil - Fatal编程技术网

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,但没有传递任何参数。