0

I need to open a HTML page in the web browser using Python. I am doing this with the webbrowser lib and webbrowser.open(url, new = 2) and actually it works pretty fine. But because I want to open the page from a file and I also want to export my project to other computers the URL is not always the same, according to where the user stored my project. At the moment I am using this

url = r"file:///C:/Users/xyx/Desktop/Application/Main/TestApp/index-release.html"

But I need to do something like this:

url = r"file:///../Javascript/Main/TestApp/index-release.html"

With the dots for going a directory back(in what ever dir my application is stored and then into the afterwards given path. But if I do it like this, it does not work and just opens a "about:blank" page. Thanks for your help.

ksai
  • 987
  • 6
  • 18
Cyaena
  • 15
  • 7
  • 1
    perhaps look at this for getting your file path. https://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory – Sasha Aug 04 '17 at 09:20

1 Answers1

0

You need to absolutize your path before creating the file URL. Something like:

import os

path = '../Javascript/Main/TestApp/index-release.html'
url = 'file:///' + os.path.abspath(path)
Jonathan Eunice
  • 21,653
  • 6
  • 75
  • 77