Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get all stdout as a string:</p> <pre><code>from subprocess import check_output as qx cmd = r'C:\Tools\Dvb_pid_3_0.exe' output = qx(cmd) </code></pre> <p>To get both stdout and stderr as a single string:</p> <pre><code>from subprocess import STDOUT output = qx(cmd, stderr=STDOUT) </code></pre> <p>To get all lines as a list:</p> <pre><code>lines = output.splitlines() </code></pre> <p>To get lines as they are being printed by the subprocess:</p> <pre><code>from subprocess import Popen, PIPE p = Popen(cmd, stdout=PIPE, bufsize=1) for line in iter(p.stdout.readline, ''): print line, p.stdout.close() if p.wait() != 0: raise RuntimeError("%r failed, exit status: %d" % (cmd, p.returncode)) </code></pre> <p>Add <code>stderr=STDOUT</code> to the <code>Popen()</code> call to merge stdout/stderr.</p> <p>Note: if <code>cmd</code> uses block-buffering in the non-interactive mode then lines won't appear until the buffer flushes. <a href="https://bitbucket.org/geertj/winpexpect/wiki/Home" rel="noreferrer"><code>winpexpect</code></a> module might be able to get the output sooner.</p> <p>To save the output to a file:</p> <pre><code>import subprocess with open('output.txt', 'wb') as f: subprocess.check_call(cmd, stdout=f) # to read line by line with open('output.txt') as f: for line in f: print line, </code></pre> <p>If <code>cmd</code> always requires input even an empty one; set <code>stdin</code>:</p> <pre><code>import os with open(os.devnull, 'rb') as DEVNULL: output = qx(cmd, stdin=DEVNULL) # use subprocess.DEVNULL on Python 3.3+ </code></pre> <p>You could combine these solutions e.g., to merge stdout/stderr, and to save the output to a file, and to provide an empty input:</p> <pre><code>import os from subprocess import STDOUT, check_call as x with open(os.devnull, 'rb') as DEVNULL, open('output.txt', 'wb') as f: x(cmd, stdin=DEVNULL, stdout=f, stderr=STDOUT) </code></pre> <p>To provide all input as a single string you could use <code>.communicate()</code> method:</p> <pre><code>#!/usr/bin/env python from subprocess import Popen, PIPE cmd = ["python", "test.py"] p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True) stdout_text, stderr_text = p.communicate(input="1\n\n") print("stdout: %r\nstderr: %r" % (stdout_text, stderr_text)) if p.returncode != 0: raise RuntimeError("%r failed, status code %d" % (cmd, p.returncode)) </code></pre> <p>where <code>test.py</code>:</p> <pre><code>print raw_input('abc')[::-1] raw_input('press enter to exit') </code></pre> <p>If your interaction with the program is more like a conversation than you might need <a href="https://bitbucket.org/geertj/winpexpect/wiki/Home" rel="noreferrer"><code>winpexpect</code> module</a>. Here's an <a href="http://www.noah.org/wiki/Pexpect#Overview" rel="noreferrer">example from <code>pexpect</code> docs</a>:</p> <pre><code># This connects to the openbsd ftp site and # downloads the recursive directory listing. from winpexpect import winspawn as spawn child = spawn ('ftp ftp.openbsd.org') child.expect ('Name .*: ') child.sendline ('anonymous') child.expect ('Password:') child.sendline ('noah@example.com') child.expect ('ftp&gt; ') child.sendline ('cd pub') child.expect('ftp&gt; ') child.sendline ('get ls-lR.gz') child.expect('ftp&gt; ') child.sendline ('bye') </code></pre> <p>To send special keys such as <code>F3</code>, <code>F10</code> on Windows you might need <a href="http://www.rutherfurd.net/python/sendkeys/" rel="noreferrer"><code>SendKeys</code> module</a> or its pure Python implementation <a href="http://code.google.com/p/sendkeys-ctypes/" rel="noreferrer"><code>SendKeys-ctypes</code></a>. Something like:</p> <pre><code>from SendKeys import SendKeys SendKeys(r""" {LWIN} {PAUSE .25} r C:\Tools\Dvb_pid_3_0.exe{ENTER} {PAUSE 1} 1{ENTER} {PAUSE 1} 2{ENTER} {PAUSE 1} {F3} {PAUSE 1} {F10} """) </code></pre> <p>It doesn't capture output.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload