tags:

views:

489

answers:

3

The following doesn't work, because it doesn't wait until the process is finished:

import subprocess
p = subprocess.Popen('start /WAIT /B MOZILL~1.LNK', shell=True)
p.wait()

Any idea how to run a shortcut and wait that the subprocess returns ?

Edit: originally I was trying this without the shell option in my post, which caused Popen to fail. In effect, start is not an executable but a shell command. This was fixed thanks to Jim.

+4  A: 

You will need to invoke a shell to get the subprocess option to work:

p = subprocess.Popen('start /B MOZILL~1.LNK', shell=True)
p.wait()

This however will still exit immediately (see @R. Bemrose).

If p.pid contains the correct pid (I'm not sure on windows), then you could use os.waitpid() to wait for the program to exit. Otherwise you may need to use some win32 com magic.

JimB
This solved partially the problem. Now the subprocess.Popen command works. But still p.wait() returns as soon as the process was launched.
Mapad
try p.pid and os.waitpid() - updated answer
JimB
p.pid is correct, but os.waitpid won't work. I guess you're right, I need to go for win32process module, and run the process through this interface. I am not done yet, but I get closer. Thanks for your help!
Mapad
A: 

Note: I am simply adding on Jim's reply, with a small trick. What about using 'WAIT' option for start?

p = subprocess.Popen('start /B MOZILL~1.LNK /WAIT', shell=True)
p.wait()

This should work.

Roberto Liffredo
Unfortunately this doesn't change anything
Mapad
+3  A: 

cmd.exe is terminating as soon as start launches the program. This behavior is documented (in start /? ):

If Command Extensions are enabled, external command invocation through the command line or the START command changes as follows:

...

When executing an application that is a 32-bit GUI application, CMD.EXE does not wait for the application to terminate before returning to the command prompt. This new behavior does NOT occur if executing within a command script.

How this is affected by the /wait flag, I'm not sure.

R. Bemrose