[Tutor] Pipe variable to external command (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu May 12 20:24:15 CEST 2005


> http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/68786
> As a work around, I end up calling popen3 like this:
>
> Open3.popen3 ("/some/command/to/run ; echo $? 1>&2") { ... }
>
> That ensures the exit code from /some/command/to/run will be the last
> line of the standard error stream from Open3.popen3. Then I can just
> pull that line off the output to get the return code.


Hi Jeffrey,

Ah!  Ok, you're looking for the error code too.



> Is there a more elegant way to get the exitcode off the child process,
> or is this the "approved" method?

Yeah; check the Popen3 object in the 'popen2' module: it has a separate
way of getting at the error code:

    http://www.python.org/doc/lib/popen3-objects.html


For example:

######
>>> import popen2
>>> process = popen2.Popen3(['wc'])
>>> process.tochild.write("hello\nworld")
>>> process.tochild.close()
>>> process.fromchild.read()
'      1       2      11\n'
>>> process.fromchild.close()
>>> process.wait()
0
######


Hope this helps!



More information about the Tutor mailing list