Search code examples
pythonsubprocess

How can I remove the extra output from the subprocess call?


My code is making an API request. In response, I get json and an additional output. How can I remove extra output from a response?

I use Linux OS and Python 3

import subprocess

url = "curl -k -u '{user}':'{password}' {url}api/v1/clusters/{cl_name}/alerts?fields=*\&Alert/state.in\(WARNING,CRITICAL\)\&sortBy=Alert/state".format(user=self.user, password=self.passwd, url=cluster.url, cl_name=cluster.name)
resp = subprocess.check_output(url,stderr=subprocess.STDOUT, shell=True)

response looks like this:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   603    0   603    0     0   1419      0 --:--:-- --:--:-- --:--:--  1470
14:03
{
        "cluster_name" : "test_clust1",
        "component_name" : "DATANODE",
        "definition_id" : 18,
        "definition_name" : "datanode_storage"
}

Solution

  • Yes, you should redirect stderr log to null. 2>/dev/null - after the command