subprocess.call输入是int。我们怎么能把它变成弦?

时间:2018-01-31 15:55:21

标签: python python-3.x subprocess

我一直在试图查看“ps -A”命令中是否找到“网络”服务。但是,得到错误:“TypeError:类型'int'的参数不可迭代”。这是我的代码:

import subprocess
tmp = subprocess.call('ps -A', shell=True, stderr=subprocess.DEVNULL)
if 'networking' in tmp:
    print("networking service is up an running!")
else:
    print("networking service is not up :(")

即使在subprocess.call function =>末尾使用.decode()也是如此。它说这里不支持.decode()...

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

subprocess.call返回一个0的整数,意味着一切正常。

尝试subprocess.check_output

import subprocess
p = subprocess.check_output('ps -A', shell=True, stderr=subprocess.DEVNULL)
if b'networking' in p:
    print("networking service is up an running!")
else:
    print("networking service is not up :(")