2017-08-21 34 views
0

使用check_output函数时出现警告,我无法重定向警告。我认为stderr只会带来错误,subprocess.check_output不能处理警告(虽然不确定)。任何人都可以建议需要重定向python 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 
+0

为什么不解决与OpenStack的问题,而不是在你的Python脚本中解决它呢? :D – AK47

+1

它运行在我没有访问的其他服务器上。 –

+0

警告应该在'stdin'或'stderr'中。没有像“stdwarn”这样的想法。你会在哪里看到警告? – sauerburger

回答

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() 
+0

这没有用。我已经尝试过了。 :( –