首页 > 解决方案 > 你如何从 Python 中“启动”一个 Windows 协议?

问题描述

我们有一个 python 脚本需要触发 Microsoft Store 的打开。我们认为最简单的方法是使用ms-windows-store://协议。

我们目前正在这样做

import subprocess
ret = subprocess.call(["start", "ms-windows-store://pdp/?ProductId=9WZDNCRFHVJL"], shell=True)

这是推荐的方法吗?我不确定在start这里使用是否正确,或者是否有更好的方法?

标签: python-3.xwindows

解决方案


使用os.startfile("ms-windows-store://pdp/?ProductId=9WZDNCRFHVJL"). 这会直接调用 WINAPI ShellExecuteW。如果您使用子进程,则需要启动子进程。加上 CMD 的start命令会首先搜索PATH找到它可以执行的文件。假设没有找到任何东西(给定这个名称,可能什么也找不到),它会将请求交给ShellExecuteExWOS shell 来处理它。


推荐阅读