Run a shortcut under windows

后端 未结 3 914
春和景丽
春和景丽 2021-01-03 15:22

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\',          


        
相关标签:
3条回答
  • 2021-01-03 15:56

    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.

    0 讨论(0)
  • 2021-01-03 16:03

    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.

    0 讨论(0)
  • 2021-01-03 16:03

    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.

    0 讨论(0)
提交回复
热议问题