[Tutor] Interacting with stderr
Danny Yoo
dyoo at hashcollision.org
Thu Aug 28 03:56:48 CEST 2014
> Crude and incomplete and untested example:
>
> from subprocess import Popen, PIPE
>
> P = Popen("avconv ... lots of arguments...", shell=True, stderr=PIPE)
>
> for line in P.stderr:
> ... examine the line from stderr ...
>
> # ok, we have read all of stderr now
> xit = P.wait()
> if xit != 0:
> ... command was unsuccessful, complain, maybe abort ...
The subprocess documentation has a few good examples of pipelines that
should apply to this scenario. I'd recommend the original questioner
look at the documentation here closely, because he or she is using a
feature of 'subprocess' that is a bit iffy, namely, the use of
"shell=True". Try to avoid "shell=True" unless you really have no
choice.
Rather than construct the pipeline through the shell, do it through
Python if you can. See:
https://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline
Also, prefer the use of communicate() rather than wait() in the
scenario above. Otherwise, the code is susceptible to PIPEs getting
overflowed. See:
https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate
Best of wishes!
More information about the Tutor
mailing list