Calling BASH from Python

From XPUB & Lens-Based wiki
Revision as of 11:00, 28 October 2011 by Michael Murtaugh (talk | contribs) (Created page with "<source lang="python"> def system_stdin_stderr (cmd, readlimitbytes=4000): p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subproc...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
def system_stdin_stderr (cmd, readlimitbytes=4000):
    p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
    (pin, pout) = (p.stdin, p.stdout)
    results = pout.read(readlimitbytes)
    pin.close(); pout.close()
    os.kill(p.pid, 1)
    return results