rag
rag

Reputation: 21

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

Upvotes: 2

Views: 21304

Answers (4)

agf
agf

Reputation: 176980

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.

Upvotes: 7

David Heffernan
David Heffernan

Reputation: 613562

The error message indicates that process.CommandLine is returning None for some reason.

Upvotes: 1

Adrien Plisson
Adrien Plisson

Reputation: 23313

it means that c is None after the call to process.CommandLine. since c is None, it cannot be iterated over, so the ifstatement which follows, which iterate over c and tries to compare each items of c to 'DomainManager', cannot execute and throws an exception.

Upvotes: 1

unutbu
unutbu

Reputation: 880877

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:

Upvotes: 2

Related Questions