close

How to open an HTML file in the browser from Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to open an HTML file in the browser from Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to open an HTML file in the browser from Python?

  1. How to open an HTML file in the browser from Python?

    Try specifying the “file://” at the start of the URL.
    // Also, use the absolute path of the file: webbrowser.open('file://' + os.path.realpath(filename))

  2. open an HTML file in the browser from Python

    Try specifying the “file://” at the start of the URL.
    // Also, use the absolute path of the file: webbrowser.open('file://' + os.path.realpath(filename))

Method 1

Try specifying the “file://” at the start of the URL.

// Also, use the absolute path of the file:

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

Or

import webbrowser
new = 2 # open in a new tab, if possible

// open a public URL, in this case, the webbrowser docs
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.open(url,new=new)

// open an HTML file on my own (Windows) computer
url = "file://d/testdata.html"
webbrowser.open(url,new=new)

Method 2

import os
os.system("start [your's_url]")

Enjoy!

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read