Launching a shortcut under windows

The following does not work as it does not wait for the process to complete:

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

      

Any idea of ​​running a shortcut and waiting for the subprocess to return?

Edit: I originally tried this without the shell option in my post, which caused Popen to crash. In fact, start

it is not executable, but a shell command. This was fixed thanks to Jim.

+1


source to share


3 answers


You will need to call the shell to be able to use the subprocess:

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

      



This, however, will still come out immediately (see @R. Bemrose).

If p.pid

contains the correct pid (I'm not sure about windows) you can use os.waitpid()

to wait for the program to exit. Otherwise, you may need to use some win32 com magic.

+4


source


cmd.exe terminates as soon as startup starts the program. This behavior is documented (at the beginning of /?):

If command extensions are enabled, invoking an external command via the command line or START command changes as follows:

...



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

How this affects the / wait flag I'm not sure.

+3


source


Note. I'm just adding to Jim's answer with a little trick. How about the 'WAIT' option to run?

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

      

This should work.

0


source







All Articles