Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a straightforward port of <a href="http://linux.die.net/man/1/tee" rel="nofollow noreferrer"><code>tee</code></a> to Python.</p> <pre><code>import sys sinks = sys.argv[1:] sinks = [open(sink, "w") for sink in sinks] sinks.append(sys.stderr) while True: input = sys.stdin.read(1024) if input: for sink in sinks: sink.write(input) else: break </code></pre> <p>I'm running on Linux right now but this ought to work on most platforms.</p> <hr> <p>Now for the <code>subprocess</code> part, I don't know how you want to 'wire' the subprocess's <code>stdin</code>, <code>stdout</code> and <code>stderr</code> to your <code>stdin</code>, <code>stdout</code>, <code>stderr</code> and file sinks, but I know you can do this:</p> <pre><code>import subprocess callee = subprocess.Popen( ["python", "-i"], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE ) </code></pre> <p>Now you can access <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.stdin" rel="nofollow noreferrer"><code>callee.stdin</code></a>, <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.stdout" rel="nofollow noreferrer"><code>callee.stdout</code></a> and <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.stderr" rel="nofollow noreferrer"><code>callee.stderr</code></a> like normal files, enabling the above "solution" to work. If you want to get the <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.returncode" rel="nofollow noreferrer"><code>callee.returncode</code></a>, you'll need to make an extra call to <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.poll" rel="nofollow noreferrer"><code>callee.poll()</code></a>.</p> <p>Be careful with writing to <code>callee.stdin</code>: if the process has exited when you do that, an error may be rised (on Linux, I get <code>IOError: [Errno 32] Broken pipe</code>).</p>
 

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