Grabbing the output of a long-winded shell call (in GNU/Linux)

Diez B. Roggisch deets at nospam.web.de
Tue May 1 16:20:51 EDT 2007


Efrat Regev schrieb:
> draghuram at gmail.com wrote:
>> On May 1, 2:23 pm, Efrat Regev <efrat_re... at yahoo.com> wrote:
>>
>>> So my question is if there's a way to "grab" the output as it's being
>>> generated. It doesn't matter if the solution is blocking (as opposed to
>>> callback based), since threads can handle this. I just don't know how to
>>> "grab" the output. I appreciate your time in reading (and answering
>>> this), as I've been googling several hours for this.
>>
>> There may be more pythonic solution than what I suggest here but this
>> is what I have done when I needed similar functionality. Basically run
>> your command in the background and redirect its stdout/err to a temp
>> file. You may run the command either in the background or in a
>> separate thread. You can then run the command "tail --retry --
>> pid=<pid> -n+0 -F <output_file>" and grab the output. The tail command
>> exits once the real command is done.

Or instead use the python subprocess module and read the commands 
stdin/out/err from the Popen-object.

Diez



More information about the Python-list mailing list