popen4 - get exit status

Tim Johnson tim at akwebsoft.com
Mon Aug 27 19:44:59 EDT 2012


* Dave Angel <d at davea.name> [120827 15:20]:
> On 08/27/2012 06:39 PM, Tim Johnson wrote:
> > In bash I do the following:
> > linus:journal tim$ /home/AKMLS/cgi-bin/perl/processJournal-Photo.pl hiccup
> > -bash: /home/AKMLS/cgi-bin/perl/processJournal-Photo.pl: No such file or directory
> > linus:journal tim$ echo $?
> > 127
> >
> > In python, use os.popen4 I do the following:
> >>>> fin,fout = os.popen4('/home/AKMLS/cgi-bin/perl/processJournal-Photo.pl hiccup;echo $?')
> >>>> results = fout.readlines()
> >>>> results
> > ['/bin/sh: /home/AKMLS/cgi-bin/perl/processJournal-Photo.pl: No such file or directory\n', '127\n']
> >
> > Well, I got the exit code as the last item in the results, but I'm wondering if
> > there is a better way. From help(os) - I don't find any variables dedicated to
> > holding exit status.
> 
> According to:
>     http://docs.python.org/library/popen2.html
> <http://docs.python.org/library/popen2.html>
> 
> " The only way to retrieve the return codes for the child processes is
> by using the poll() or wait() methods on the Popen3
> <http://docs.python.org/library/popen2.html#popen2.Popen3> and Popen4
> <http://docs.python.org/library/popen2.html#popen2.Popen4> classes;
> these are only available on Unix. This information is not available when
> using the popen2()
> <http://docs.python.org/library/popen2.html#module-popen2>, popen3()
> <http://docs.python.org/library/popen2.html#popen2.popen3>, and popen4()
> <http://docs.python.org/library/popen2.html#popen2.popen4> functions,...."
> 
> However, unless you're using an old version of Python (2.5 or below),
> you should be using the subprocess module.
   Thanks DaveA and see my reply to Benjamin
   that will do it.
-- 
Tim 
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com



More information about the Python-list mailing list