How to open a .html file

Question:

for a little project i’m trying to create a python program which allows me to run it and then go to open files in html format that I created earlier.
But i need that the files are opened in the "browser" and not in the visual code terminal.

this is my code:

import os 
RdFile = open(link.html, 'r')
RdFile = RdFile.readline()
for row in Rdfile:
    print(row),
input()

and that open in the shell the file
so I was wondering how to make the python file run to open the file directly in its format and not from the terminal.

Thanks

Asked By: TortelloSte

||

Answers:

You are making your code print the lines that are inside the file.

This should work:

import webbrowser

RdFile = webbrowser.open(r'path')  #Full path to your file
Answered By: TheDiamondCreeper
# creating nd viewing the html files in python
  
import webbrowser
import os
  
# to open/create a new html file in the write mode
f = open('GFG.html', 'w')
  
# the html code which will go in the file GFG.html
html_template = """
<html>
<head></head>
<body>
<p>GFG</p>
  
</body>
</html>
"""
# writing the code into the file
f.write(html_template)
  
# close the file
f.close()
  
# 1st method how to open html files in chrome using
filename = 'file:///'+os.getcwd()+'/' + 'GFG.html'
webbrowser.open_new_tab(filename) 
Answered By: Ravi Mounika
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.