1

有没有办法检测运行脚本的系统上是否有可用的浏览器?在服务器上运行以下代码时没有任何反应:

try:
    webbrowser.open("file://" + os.path.realpath(path))
except webbrowser.Error:
    print "Something went wrong when opening webbrowser"

奇怪的是没有捕获异常,也没有打开浏览器。我正在通过 SSH 连接从命令行运行脚本,而且我对服务器相关的东西不是很精通,所以可能有另一种方法可以检测到我缺少的东西。

谢谢!

4

1 回答 1

2

查看文档

网页浏览器。得到([名称])

返回浏览器类型名称的控制器对象。如果 name 为空,则返回适合调用者环境的默认浏览器的控制器。

这对我有用:

try:
    # we are not really interested in the return value
    webbrowser.get()
    webbrowser.open("file://" + os.path.realpath(path))
except Exception as e:
    print "Webbrowser error: " % e

输出:

Webbrowser error: could not locate runnable browser
于 2016-09-07T13:58:39.913 回答