windows下运行快捷方式 [英] Run a shortcut under windows

查看:41
本文介绍了windows下运行快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下不起作用,因为它不会等到过程完成:

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 ?

最初我在我的帖子中没有 shell 选项的情况下尝试这个,这导致 Popen 失败.实际上,start 不是一个可执行文件,而是一个 shell 命令.多亏了 Jim,这个问题得到了解决.

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.

推荐答案

你需要调用一个 shell 来让 subprocess 选项工作:

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()

然而这仍然会立即退出(见@R. Bemrose).

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

如果 p.pid 包含正确的 pid(我不确定在 Windows 上),那么你可以使用 os.waitpid() 等待程序退出.否则你可能需要使用一些 win32 com 魔法.

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.

这篇关于windows下运行快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆