我在python中有以下代码:
i = call(["salt-cloud", "-m", fileName, "--assume-yes"])
print (i)i始终为0,因为操作已经完成。
问题是我想得到这个操作的输出。在我们的案例中:
window:
----------
Error:
----------
Not Deployed:
Failed to start Salt on host window是运行salt-cloud -m fileName --assume-yes的输出,它表示在此过程中引发的错误,我想知道。
我如何在Python中实现它呢?
发布于 2015-03-30 13:31:01
使用check_output并捕获将引发任何非零退出状态的CalledProcessError:
from subprocess import check_output, CalledProcessError
try:
out = check_call(["salt-cloud", "-m", fileName, "--assume-yes"])
print(out)
except CalledProcessError as e:
print(e.message)如果使用python < 2.7,则需要在python2.7中添加Popen、check_call和check_output:
from subprocess import Popen, PIPE
p = Popen(["salt-cloud", "-m", filename, "--assume-yes"],stderr=PIPE,stdout=PIPE)
out, err = p.communicate()
print( err if err else out)发布于 2015-03-30 13:26:07
假设您使用的是subprocess.call,您可能想看看输出(),它以字节字符串的形式返回子进程的输出。
https://stackoverflow.com/questions/29347376
复制相似问题