I am writting a Python script and I am running out of time. I need to do some things that I know pretty well in bash, so I just wonder how can I embed some bash lines into a Python script.
The ideal way to do it:
def run_script(script, stdin=None): """Returns (stdout, stderr), raises error on non-zero return code""" import subprocess # Note: by using a list here (['bash', ...]) you avoid quoting issues, as the # arguments are passed in exactly this order (spaces, quotes, and newlines won't # cause problems): proc = subprocess.Popen(['bash', '-c', script], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) stdout, stderr = proc.communicate() if proc.returncode: raise ScriptException(proc.returncode, stdout, stderr, script) return stdout, stderr class ScriptException(Exception): def __init__(self, returncode, stdout, stderr, script): self.returncode = returncode self.stdout = stdout self.stderr = stderr Exception.__init__('Error in script')
You might also add a nice
__str__ method to
ScriptException (you are sure to need it to debug your scripts) -- but I leave that to the reader.
If you don't use
stdout=subprocess.PIPE etc then the script will be attached directly to the console. This is really handy if you have, for instance, a password prompt from ssh. So you might want to add flags to control whether you want to capture stdout, stderr, and stdin.