How to know if a popen*() was successful?

Eric Brunel eric.brunel at pragmadev.com
Tue May 21 06:28:32 EDT 2002


Fernando Perez wrote:

> I'm failing at detecting whether a process has indeed been opened by a
> popen4 call. Consider the following code:
> 
> pager_cmd = 'less'
> pager,shell_out = os.popen4(pager_cmd,'w')
> 
> How can I tell if less was indeed opened?
[snip]

On Unix, there's a simple way: use the Popen classes instead of the popen 
functions and use the "poll" method:

import popen2
pager_cmd = 'less'
p = popen2.Popen4(pager_cmd)
if p.poll() != -1:
  print "%s doesn't exist" % pager_cmd
else:
  pager, shell_out = p.tochild, p.fromchild

> ps. I'm testing on linux, but this code needs to be multiplatform, hence
> my need to check ok. If less isn't present, I default to a python-written
> dumb pager for platforms like windows.

Don't know how to do it on Windoze... [BTW, the Popen classes would be very 
helpful on Windows too: they offer many things that you just can't do if 
you use the popen functions. Any plans to port the classes on Windows?]

HTH anyway...
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com




More information about the Python-list mailing list