TypeError:&不支持的操作数类型:“ str”和“ str”如何解决

时间:2019-09-18 07:34:40

标签: python python-3.x

我需要统计正在运行多少个具有相同名称“ chrome.exe”的进程。

这是我的python脚本

import wmi
c = wmi.WMI ()

for process in c.Win32_Process ():
    #print (process.Name)
    if process.Name == "chrome.exe" & process.Name > 1:
        print (process.Name)

当我运行该脚本时,我得到的是这样的:

chrome.exe
chrome.exe
chrome.exe
chrome.exe
chrome.exe
chrome.exe
chrome.exe
chrome.exe

但是我需要获取数量上的数量,如果数量大于1,则打印出一些东西,并且也会出现此错误

  

TypeError:&不支持的操作数类型:“ str”和“ str”

1 个答案:

答案 0 :(得分:0)

哦,男孩,这里有点乱。 提出问题:您需要将“ &”更改为“ and

其他:

for process in c.Win32_Process ():-> for process in c.Win32_Process():

print (process.Name)-> print(process.Name)

最后:

i = 1 # or 0, do as you need
for process in c.Win32_Process():
    if process.Name == "chrome.exe" and i > 1:
        print(process.Name)
    i += 1 # there are more elegenat ways to do this, but let's keep this simple