Python 类型错误:';波本';对象不可调用

Python 类型错误:';波本';对象不可调用,python,python-3.x,subprocess,pywin32,Python,Python 3.x,Subprocess,Pywin32,我正在尝试使用子流程模块的“Popen”方法查询windows服务的状态。但是我越来越 TypeError:“Popen”对象不可调用 import subprocess, codecs def serviceStatus(RadiaService): status = [] cmd = 'sc query ' + RadiaService pDetails = subprocess.Popen(cmd, shell = True, stdout = subproces

我正在尝试使用子流程模块的“Popen”方法查询windows服务的状态。但是我越来越

TypeError:“Popen”对象不可调用

import subprocess, codecs

def serviceStatus(RadiaService):
    status = []
    cmd = 'sc query ' + RadiaService
    pDetails = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE)
    for item in pDetails():
        status.append(item)
    finalStatus = b''.join(status).decode('utf-8')
    print(finalStatus)

if __name__ == '__main__':
    serviceStatus('RCA')
错误跟踪:

Traceback (most recent call last):
  File "C:\Alen\Cumulative RHF\Radia_Cumulative_Patch\cumulativeHotFixproject\lib\win32.py", line 39, in <module>
    serviceStatus('RCA')
  File "C:\Alen\Cumulative RHF\Radia_Cumulative_Patch\cumulativeHotFixproject\lib\win32.py", line 33, in serviceStatus
    for item in pDetails():
TypeError: 'Popen' object is not callable
回溯(最近一次呼叫最后一次):
文件“C:\Alen\Cumulative RHF\Radia\u Cumulative\u Patch\cumulativeHotFixproject\lib\win32.py”,第39行,在
服务状态(“RCA”)
文件“C:\Alen\Cumulative RHF\Radia\u Cumulative\u Patch\cumulativeHotFixproject\lib\win32.py”,第33行,处于服务状态
对于pDetails()中的项:
TypeError:“Popen”对象不可调用

看起来您希望收集子流程的标准输出。您必须使用
pDetails.stdout
。以下是一个帮助您入门的示例:

import subprocess
p = subprocess.Popen("ls -la", shell=True, stdout=subprocess.PIPE)
output = b''.join(p.stdout).decode('utf-8')
print(output)
基于此,您的代码应该是这样的:

import subprocess, codecs

def serviceStatus(RadiaService):
    cmd = 'sc query ' + RadiaService
    pDetails = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE)
    return b''.join(pDetails.stdout).decode('utf-8')

def main():
    print(serviceStatus('RCA'))

if __name__ == '__main__':
    main()

注意:您不必收集列表中的输出,您可以直接将iterable馈送到join。如果您需要一个列表,您仍然不必使用for循环,您只需编写
status=list(pDetails.stdout)

尝试从pDetails()中的“for item in pDetails():”行中的pDetails()中删除括号,使其变为“for item in pDetails:”