webbrowser.open()在python中 [英] webbrowser.open() in python

查看:234
本文介绍了webbrowser.open()在python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python文件 html_gen.py 它写了一个新的 html 文件索引.html 放在同一个目录中,并且希望在写作完成时打开 index.html

I have a python file html_gen.py which write a new html file index.html in the same directory, and would like to open up the index.html when the writing is finished.

所以我写了

So I wrote

import webbrowser
webbrowser.open("index.html");

执行.py文件后没有任何事情发生。如果我代之以放置代码

But nothing happen after executing the .py file. If I instead put a code

webbrowser.open("http://www.google.com")

执行代码时,Safari将打开google frontpage。

Safari will open google frontpage when executing the code.

我想知道如何打开本地的index.html文件?

I wonder how to open the local index.html file?

推荐答案

尝试在开头指定file://网址。另外,使用文件的绝对路径:

Try specifying the "file://" at the start of the URL. Also, use the absolute path of the file:

import webbrowser, os
webbrowser.open('file://' + os.path.realpath(filename))

这篇关于webbrowser.open()在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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