Note that there are some explanatory texts on larger screens.

plurals
  1. POPython subprocess: Why does stdin=PIPE change the output of some commands?
    text
    copied!<p>Some interactive commands output differently if stdin is piped. Why is that?</p> <p>Below I test subprocess.Popen on 3 different commands, with and without stdin piped.</p> <p>&nbsp;<br> <strong>Code:</strong></p> <pre class="lang-python prettyprint-override"><code>import subprocess, time def run_command(command, enable_input): print 'command="{}", enable_input={}:'.format(command, enable_input) # Launch the process and set up pipes. if enable_input: stdin = subprocess.PIPE else: stdin = None child = subprocess.Popen(command, stdin=stdin) # Wait a second for output. time.sleep(1) # Terminate the child if it hasn't finished. if child.poll() == None: child.terminate() print '\n-----' # Print a separator commands = ('cmd', 'python', 'timeout 1') for command in commands: run_command(command, enable_input=False) run_command(command, enable_input=True) </code></pre> <p>&nbsp;<br> <strong>Output:</strong></p> <pre class="lang-none prettyprint-override"><code>command="cmd", enable_input=False: Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\&gt; ----- command="cmd", enable_input=True: Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\&gt; ----- command="python", enable_input=False: Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; ----- command="python", enable_input=True: ----- command="timeout 1", enable_input=False: Waiting for 0 seconds, press a key to continue ... ----- command="timeout 1", enable_input=True: ERROR: Input redirection is not supported, exiting the process immediately. ----- </code></pre> <p>&nbsp;<br> Answers to the question linked below suggest that some programs try to detect whether they're being run by a human or a script. Is that the case here? And if so, how do they detect that on Windows?</p> <p><a href="https://stackoverflow.com/questions/2356391/why-does-supplying-stdin-to-subprocess-popen-cause-what-is-written-to-stdout-to">Why does supplying stdin to subprocess.Popen cause what is written to stdout to change?</a></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