Created
May 4, 2017 07:49
-
-
Save Mykolaichenko/889f4d342cebdfb07adda4c472a983c7 to your computer and use it in GitHub Desktop.
Python wrapper for shell commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Wrapper for shell command in Python | |
from subprocess import Popen | |
class ShellCommand(object): | |
def __init__(self, command): | |
self.command = command | |
self.stdout = None | |
self.stderr = None | |
self.code = None | |
def execute(self): | |
p = Popen(self.command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | |
self.stdout, self.stderr = p.communicate() | |
self.code = p.returncode | |
return self.stdout, self.stderr, self.code | |
# Example: | |
# stdout, stderr, code = ShellCommand("ls -lhta /root").execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment