Python subprocess.call输入为int。如何使其成为字符串?
我一直试图简单地看看“ps-A”命令中是否有“networking”服务。但是,获取错误:“TypeError:类型为'int'的参数不可iterable”。这是我的代码:Python subprocess.call输入为int。如何使其成为字符串?,python,python-3.x,subprocess,Python,Python 3.x,Subprocess,我一直试图简单地看看“ps-A”命令中是否有“networking”服务。但是,获取错误:“TypeError:类型为'int'的参数不可iterable”。这是我的代码: import subprocess tmp = subprocess.call('ps -A', shell=True, stderr=subprocess.DEVNULL) if 'networking' in tmp: print("networking service is up an running!") e
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 :(")
即使在子流程.call
函数=>末尾使用.decode()时,它也会说这里不支持.decode()
有什么想法吗?
子流程。call
返回一个带0
的整数,表示一切正常
尝试:
注意:您也可以指定编码kwarg,因此不需要bytestring文本+1是的。尝试了这个,但是在MacOS X上的utf-8出现了一个解码错误。这是一个输入错误<代码>UnicodeDecodeError:“utf-8”编解码器无法解码位置34772处的字节0xc2:无效的继续字节正常。在Python 3中,
子进程调用(…,universal\u newlines=True)
透明地处理编码问题。您还需要考虑使用3.5切换到subprocess.run()
+
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 :(")