简体   繁体   中英

How to get the output from executing shell commands in Python

I am trying to execute this shell command using python

but the problem is, it doesn't give the output even if there is a wrong or not:

This is what I have tried:

get_ns_p1 = subprocess.Popen(['kubectl', 'get', 'ns'], stdout=subprocess.PIPE)
get_ns_p2 = subprocess.Popen(["grep", "-E", "\'(^|\s)"+NAMESPACE+"($|\s)\'"], stdin=get_ns_p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

get_ns_p1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
out_ns, err = get_ns_p2.communicate()

print("output: " + out_ns)
print("error: " + err)

The output looks like:

output:

error:

but in the terminal itseld, it shows an output like this:

Error from server (AlreadyExists): namespaces "namespace-tests" already exists

how can I this error to my err variable?

Your code works correctly; the problem is that you are attempting to capture a string which is emitted on stderr if kubectl is at all correctly written.

However, running grep as a separate subprocess is quite inefficient and unnecessary anyway; Python itself features a much more featureful and complete regular expression engine in the re library which also lets you avoid the overhead of starting a separate process.

import re

r = re.compile(r'(?:^|\s){}(?:\s|$)'.format(NAMESPACE))

kube = subprocess.run(['kubectl', 'get', 'ns'], text=True, capture_output=True)
e = []
for line in kube.stderr.split('\n'):
    if r.search(err):
        e.append(line)
err = '\n'.join(line)

If you are confined to an older version of Python which doesn't have subprocess.run() , you have to reimplement it, poorly.

p = subprocess.Popen(['kubectl', 'get', 'ns'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_ns, err = p.communicate()
e = []
for line in err.split('\n'):
    ...

This has a number of flaws and drawbacks; you really should be thinking seriously about upgrading to Python 3.5+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM