Calling BASH from Python: Difference between revisions

From XPUB & Lens-Based wiki
(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...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
A function to call a bash command, and capture stdout and sterr (text is returned).
<source lang="python">
<source lang="python">
def system_stdin_stderr (cmd, readlimitbytes=4000):
def system_stdin_stderr (cmd, readlimitbytes=4000):
Line 8: Line 10:
     return results
     return results
</source>
</source>
[[Category: BASH]] [[Category: Cookbook]] [[Category: Python]]

Latest revision as of 10:01, 28 October 2011

A function to call a bash command, and capture stdout and sterr (text is returned).

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