簡體   English   中英

TypeError:“NoneType”類型的參數在 python 中不可迭代

[英]TypeError: argument of type 'NoneType' is not iterable in python

我正在嘗試獲取正在運行的進程的 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

請告訴我原因。

謝謝, 拉格

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 是否有效,然后再嘗試檢查給定的字符串是否是它的 substring。

顯然,就 WMI 而言,某些進程沒有命令行。

它出現

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:

錯誤消息表明process.CommandLine由於某種原因正在返回None

這意味着在調用process.CommandLine之后cNone 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM