需要在python check_output中重定向警告

时间:2017-08-21 12:33:39

标签: python subprocess openstack

我在使用check_output函数时收到警告,但我无法重定向警告。我想stderr只会出错并且subprocess.check_output不能处理警告(尽管不确定)。谁有人建议?

def getSnapshot(volumeName):
    try:

        snapID, snapAttach = check_output(["openstack", "volume", "snapshot", "list", "--volume", volumeName, "-c", "Name", "-c", "ID", "-f", "value"]).rstrip().split()
        myDict['snapshot'] = [snapAttach, snapID]
    except ValueError:
        myDict['snapshot'] = None
    return myDict

警告类型:

WARNING: openstackclient.common.utils is deprecated and will be removed after Jun 2017. Please use osc_lib.utils. This warning is caused by an out-of-date import in /usr/local/lib/python2.7/dist-packages/heatclient/osc/plugin.py\nIgnoring domain related config project_domain_id because identity API version is 2.0\n

1 个答案:

答案 0 :(得分:0)

可能适合您想要的东西。 https://docs.python.org/2/library/warnings.html

    import warnings

    def fxn():
        warnings.warn("deprecated", DeprecationWarning)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        fxn()