Skip to content Skip to sidebar Skip to footer

Hold The Output Of Subprocess.Popen With A Arbitrary Varible

I'd like to retrieve the output from a shell command In [7]: subprocess.Popen('yum list installed', shell=True) Out[7]: In [8]: Loaded

Solution 1:

Try setting stdout and/or stderr to subprocess.PIPE.

import subprocess as sp

proc = sp.Popen("yum list installed", shell=True, stdout=sp.PIPE, stderr=sp.PIPE)

out = proc.stdout.read().decode('utf-8')
print(out)

As suggested in comments, it's better to use Popen.communicate() in case stderr needs reading and gets blocked. (Thanks UtahJarhead)

import subprocess as sp

cp = sp.Popen("yum list installed", shell=True, stdout=sp.PIPE, stderr=sp.PIPE).communicate()

out = cp[0].decode('utf-8')
print(out)

Post a Comment for "Hold The Output Of Subprocess.Popen With A Arbitrary Varible"