1

スクリプトが実行されているシステムで利用可能なブラウザーがあるかどうかを検出する方法はありますか? サーバーで次のコードを実行しても何も起こりません。

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

キャッチされた例外がなく、ブラウザが開いていないのは奇妙です。私は SSH 接続を介してコマンド ラインからスクリプトを実行していますが、サーバー関連の作業にあまり精通していないため、これを検出する別の方法が見つからない可能性があります。

ありがとう!

4

1 に答える 1

2

ドキュメントをチェックアウトします。

ウェブブラウザ。get ([名前])

ブラウザ タイプ名のコントローラ オブジェクトを返します。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 に答える