これは、あなたが探しているものを達成するように見える他の場所で見つけたクラスです
class Command(object):
"""Run a command and capture it's output string, error string and exit status"""
def __init__(self, command):
self.command = command
def run(self, shell=True):
import subprocess as sp
process = sp.Popen(self.command, shell = shell, stdout = sp.PIPE, stderr = sp.PIPE)
self.pid = process.pid
self.output, self.error = process.communicate()
self.failed = process.returncode
return self
@property
def returncode(self):
return self.failed
実行するには、次のようにします。
commandVar = Command("dir").run()
次に、結果を表示します。
commandVar.output