[Tutor] Does python change subprocess based on whether it is run in the interactive shell or not

Mats Wichmann mats at wichmann.us
Sat Oct 29 11:59:32 EDT 2022


On 10/29/22 07:23, Alan Gauld via Tutor wrote:
> On 28/10/2022 12:38, Nathan Smith wrote:
> 
>>       p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
>> stdin=subprocess.PIPE, stderr=subprocess.PIPE)
>>       [stdout, stderr]=p.communicate(data)
> 
> The square brackets should not be needed. Unpacking
> will work based on the comma alone.
> 
>> Where data is some data taken from io.bytesIO.read
>>
>> And cmd is a command for ffmpeg like so:
>>
>> cmd="ffmpeg/ffmpeg.exe -f wav -ac 2 -i - -f mp3 -ab 128000 - -y"
> 
> so far so good.


You don't call out the platform you're using - it's useful to be 
explicit about that. This usage form ought work okay on Windows, which 
wants a string for the command - the inclusion of ".exe" suggests it's 
Windows. It would break on POSIX platforms which can't pass arguments in 
the same string when using Popen directly, you need to use a list of 
strings instead.

So the problem isn't immediately clear to me, but can still make a 
suggestion: using "subprocess.run" is usually a fair bit easier than 
fiddling directly with the Popen constructor and communicate, and often 
"wait" and other methods to make sure everything's complete and cleaned 
up.  run handles the communication and cleanup and hands you back a nice 
CompletedProcess instance that you can grab whatever data you need from.

Have you confirmed that the value of "data" is what you expect when you 
run it as a script?




More information about the Tutor mailing list