我试图获得正在运行的进程的VM大小,并使用下面的简单脚本。在这里,我最初是想得到这个过程的参考。但得到的错误是--
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
你能告诉我原因吗。
谢谢你,拉格
发布于 2011-07-20 05:17:04
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而言,有些进程没有CommandLine。
发布于 2011-07-20 05:11:05
看上去
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:
发布于 2011-07-20 05:11:48
这意味着c
在调用process.CommandLine
之后是None
。因为c
是None
,所以不能迭代它,所以后面的if
语句(遍历c
并尝试将c
的每个项与'DomainManager'
进行比较)不能执行并抛出异常。
https://stackoverflow.com/questions/6762446
复制相似问题