According to the Python 3 documentation for subprocess.Popen, the class constructor takes an optional argument text
(which is supposed to control whether the file objects stdin, stdout and stderr are opened in text mode).
However, when I try setting text=true
upon construction of a Popen
object, I get the error
Failed: TypeError: __init__() got an unexpected keyword argument 'text'
and when I look in the source code (I'm using Python 3.6.4), the constructor takes no argument text
. What is going on here? Why does the documentation say the constructor takes an optional argument text
when it doesn't in the version of subprocess.py
that I have?
The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly. Run the command described by args. Wait for command to complete, then return a CompletedProcess instance.
Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.
The main difference is that subprocess. run() executes a command and waits for it to finish, while with subprocess. Popen you can continue doing your stuff while the process finishes and then just repeatedly call Popen. communicate() yourself to pass and receive data to your process.
Popen Function The function should return a pointer to a stream that may be used to read from or write to the pipe while also creating a pipe between the calling application and the executed command. Immediately after starting, the Popen function returns data, and it does not wait for the subprocess to finish.
I have the feeling the text parameter has been added in 3.7, not 3.6.
Relevant part of the doc:
Changed in version 3.7: Added the text parameter, as a more understandable alias of universal_newlines. Added the capture_output parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With