469,360 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

popen and exit code on Windows

On UNIX one can use popen* to get a pipe for reading, a pipe for
writing, and the exit code of the child process via a call to close()
on the last pipe. Is there any way, in principle, to simulate such
behaviour on Windows? Some googling reveals that direct use of the
popen* functions on Windows will not do the trick, but are there
indirect ways?

Regards,
Iker

Mar 6 '06 #1
2 2458
ik************@gmail.com wrote:
On UNIX one can use popen* to get a pipe for reading, a pipe for
writing, and the exit code of the child process via a call to close()
on the last pipe. Is there any way, in principle, to simulate such
behaviour on Windows? Some googling reveals that direct use of the
popen* functions on Windows will not do the trick, but are there
indirect ways?


This is well-tested:

def launch(cmd, split_lines=True):
"""Launch a sub-process. Return its output (both stdout and stderr),
optionally split by lines (if split_lines is True). Raise a LaunchError
exception if the exit code of the process is non-zero (failure)."""
if os.name not in ['nt', 'os2']:
p = popen2.Popen4(cmd)
p.tochild.close()
if split_lines:
out = p.fromchild.readlines()
else:
out = p.fromchild.read()
ret = p.wait()
if ret == 0:
ret = None
else:
ret >>= 8
else:
i,k = os.popen4(cmd)
i.close()
if split_lines:
out = k.readlines()
else:
out = k.read()
ret = k.close()

if ret is None:
return out
raise LaunchError(ret, cmd, out)

--
Giovanni Bajo
Mar 7 '06 #2
Thanks for the reply Giovanni. Is this behaviour of the close method on
Windows documented anywhere?

Regards,
Iker

Mar 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matthew K Jensen | last post: by
1 post views Thread by rveloso | last post: by
2 posts views Thread by Greg Ercolano | last post: by
15 posts views Thread by Daniel Klein | last post: by
25 posts views Thread by Jeremy Banks | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.