TypeError: argument of type 'NoneType' is not iterable in python
I am trying to get the VM size of a running process and using the following simple script. Here initially I am trying to get the reference of that process. But getting the error as --
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
Would you please let me know the reason.
Thanks, Rag
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
Notice the condition in the if
statement:
if c is not None and "DomainManager in c":
This will check to see if c is valid before attempting to check if the given string is a substring of it.
Apparently, some processes have no CommandLine as far as WMI is concerned.
It appears
c = process.CommandLine
is setting c
equal to None
:
In [11]: "DomainManager" in None
TypeError: argument of type 'NoneType' is not iterable
I don't know anything about the Win32 API, so this is a complete guess, but you might try:
if c and "DomainManager" in c:
it means that c
is None
after the call to process.CommandLine
. since c
is None
, it cannot be iterated over, so the if
statement which follows, which iterate over c
and tries to compare each items of c
to 'DomainManager'
, cannot execute and throws an exception.
The error message indicates that process.CommandLine
is returning None
for some reason.
精彩评论