Skip to content

Instantly share code, notes, and snippets.

@Mykolaichenko
Created May 4, 2017 07:49
Show Gist options
  • Save Mykolaichenko/889f4d342cebdfb07adda4c472a983c7 to your computer and use it in GitHub Desktop.
Save Mykolaichenko/889f4d342cebdfb07adda4c472a983c7 to your computer and use it in GitHub Desktop.
Python wrapper for shell commands
# 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