Passing String Input to Subprocess.Popen via Stdin
Problem:
Passing a string into the stdin argument of subprocess.Popen using a cStringIO.StringIO object results in an error, as the object lacks the necessary fileno attribute.
Solution:
To resolve this issue, it is recommended to use the more straightforward approach outlined in the Popen.communicate() documentation. By setting stdin=PIPE, you can create a pipe for stdin and provide the string input directly to the communicate method.
1 2 3 4 5 6 7 8 9 10 |
|
Additional Note:
For Python 3.5 (3.6 for encoding), subprocess.run simplifies the process by allowing you to pass string input and retrieve the output as a string in a single call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The above is the detailed content of How to Correctly Pass String Input to `subprocess.Popen`'s stdin?. For more information, please follow other related articles on the PHP Chinese website!