TypeError:'NoneType'对象不可迭代

2 投票
4 回答
21129 浏览
提问于 2025-04-16 21:55

我正在尝试获取一个正在运行的程序的虚拟内存大小,并使用以下简单的脚本。最开始,我试图获取那个程序的引用。但是我遇到了一个错误,错误信息是 --

if "DomainManager" in c:
TypeError: argument of type 'NoneType' is not iterable

import wmi

computer = wmi.WMI ()

for process in computer.Win32_Process ():

      c = process.CommandLine
      if "DomainManager" in c:
        print c

请问你能告诉我原因吗?

谢谢,
Rag

4 个回答

1

这个错误信息说明,process.CommandLine 出现了问题,返回了 None,也就是说没有得到任何值。

2

看起来

c = process.CommandLine

正在把 c 设置为 None

In [11]: "DomainManager" in None

TypeError: argument of type 'NoneType' is not iterable

我对Win32 API一无所知,所以这只是个猜测,但你可以试试:

if c and "DomainManager" in c:
7
import wmi
computer = wmi.WMI ()

for process in computer.Win32_Process ():
    c = process.CommandLine
    if c is not None and "DomainManager" in c:
        print c

注意这个if语句里的条件:

if c is not None and "DomainManager in c":

这段代码会先检查c是否有效,然后再去判断给定的字符串是否是它的子字符串。

显然,有些进程在WMI看来是没有命令行的。

撰写回答